I am in a situation where I want to do some calculations of the size of the dom-node (upper, lower and dimensional rendering properties of the DOM node)
What I'm doing right now, according to the method, componentDidUpdate
is to call findDOMNode:
componentDidUpdate() {
var node = ReactDOM.findDOMNode(this);
this.elementBox = node.getBoundingClientRect();
this.elementHeight = node.clientHeight;
}
This works great, but I'm a little worried about work and reacting to best practices. Several places talk about using a property ref
instead of findDOMNode, but they are all intended for child dom elements, in my case I only need the DOM root of my component.
An alternative use case for ref might look like this:
render(){
return (
<section
ref={(n) => this.node = n}>
</section>
}
componentDidUpdate() {
this.elementBox = this.node.getBoundingClientRect();
this.elementHeight = this.node.clientHeight;
}
, ref dom node , , .
ββ ? ?