Lets say I have a react DOM element like this one:
render () {
...
return (
<div className = "widePaddingRight" style = {{width: "100px"}}
ref = "pickMe"
>
...
</div>
)
}
CSS:
.widePaddingRight{
paddingRight: 20px
}
If I access this element later and try to get its width like that:
ponentDidMount () {
var elem = this.refs.pickMe.getDOMNode().getBoundingClientRect()
console.log(elem.width)
}
I get 120
in my console. My expected result was 100
. Since I have to calculate with the original width I have to get the padding-attributes of the element.
Question: How can I get the paddingRight
attribute of my ponent in my react-class?
Update
With the input of @Mike 'Pomax' Kamermans I was able to solve the underlying problem (thank you for that): Just add box-sizing: border-box
to the CSS. Now getBoundingClientRect()
gives 100
instead of 120
.
I still dont know how to get a css class-attribute from a mounted div - any suggestions?
Lets say I have a react DOM element like this one:
render () {
...
return (
<div className = "widePaddingRight" style = {{width: "100px"}}
ref = "pickMe"
>
...
</div>
)
}
CSS:
.widePaddingRight{
paddingRight: 20px
}
If I access this element later and try to get its width like that:
ponentDidMount () {
var elem = this.refs.pickMe.getDOMNode().getBoundingClientRect()
console.log(elem.width)
}
I get 120
in my console. My expected result was 100
. Since I have to calculate with the original width I have to get the padding-attributes of the element.
Question: How can I get the paddingRight
attribute of my ponent in my react-class?
Update
With the input of @Mike 'Pomax' Kamermans I was able to solve the underlying problem (thank you for that): Just add box-sizing: border-box
to the CSS. Now getBoundingClientRect()
gives 100
instead of 120
.
I still dont know how to get a css class-attribute from a mounted div - any suggestions?
props
(from the parent) or state
(from the ponent itself) change occurs, at which point your render()
function is responsible for generating the "what it should now look like" ReactJS code, which React then uses to generate a diff, applying only the difference between old and new content to the DOM.
– Mike 'Pomax' Kamermans
Commented
Oct 22, 2015 at 18:14
new_width
prop during dragging (state) that is used in the render()
method.
– Benvorth
Commented
Oct 22, 2015 at 18:19
this.setState({ width: puted})
, and then you have the render function generate the appropriate content, <div style={{ width: this.state.width }} ......
– Mike 'Pomax' Kamermans
Commented
Oct 22, 2015 at 18:20
You need window.getComputedStyle
.
const style = window.getComputedStyle(React.findDOMNode(this.refs.pickMe));