The component is using jQuery to get the element width which is working. This is a question about setting the wrapper div element in such a way that its dimensions change with a resize of the window. Currently it is always 600 px by 300 px.
I am trying to change the state of an SVG React component when the parent container reaches a certain size. The container is a React HOC named withResizeHandler and the wrapper node is a div element. The div size is checked with jquery from the callback updateSize triggered by the resize event. I change the browser window size and execution enters the callback but the console log indicates that the div never changes from it original dimensions of 600px by 300px. The code is on code pen https://codepen.io/pboulos/pen/rrLYKE?editors=0111.
const withResizeHandler = (WrappedComponent) => {
return class extends React.Component {
state = {
width: 0,
height: 0
}
//Init
componentWillMount(){
console.log('HOC Resize Props',this.props);
window.addEventListener("resize", this.updateSize.bind(this));
this.setState({width:this.props.width, height:this.props.height});
}
componentDidMount() {
this.updateSize();
}
componentWillUnmount(){
$(window).off('resize');
}
updateSize = () => {
// DOM node is the wrapper div element
var node = ReactDOM.findDOMNode(this);
const nodeWidth = $(node).width();
const nodeHeight = $(node).height();
console.log("Parent Width "+ nodeWidth+" Height "+nodeHeight);
if (nodeWidth < this.props.width){
this.setState({width: nodeWidth-20});
}else{
this.setState({width: this.props.width});
}
}
render () {
return (
<div style={{"height" : "300px"}}>
{/* pass down the props from the hoc wrapper */}
<WrappedComponent width={this.state.width} height={this.state.height}/>
</div>
);
}
}
}