Check out this working demo: JSFiddle :
var Parent = React.createClass({
render: function() {
return <div id="parent">Hello Parent<Child></Child></div>;
}
});
var Child = React.createClass({
componentDidMount: function() {
alert('Parent width: ' + this.refs.child.parentNode.clientWidth);
},
render: function() {
return <div ref="child">Hello Child</div>;
}
});
The article ref="child"will make the element accessible to the component itself through this.refs.child. This is an instance of vallina node. Using this.refs.child.parentNode.clientWidthwill return the parent width. Or use this.refs.child.parentNode.getBoundingClientRect().
Link: React refs
source
share