Im new to React and stumbled across an issue I don't understand and can't resolve. I want to change an internal block size dynamically upon specific window screen width threshold cross.
Here is a sample code I prepared:
function X(title) {
const [myWidth, setMyWidth] = React.useState(200)
function onResize(){
if(innerWidth > 1000 && myWidth <= 200){
console.log(`Make Bigger: `, myWidth)
setMyWidth(400)
} else if( innerWidth <= 1000 && myWidth >= 400) {
console.log(`Make Smaller: `, myWidth)
setMyWidth(200)
}
}
React.useEffect(()=>{
console.log("HELLO")
addEventListener('resize', onResize)
},[])
return (<div>
<div style={{
backgroundColor:'red',
width: myWidth,
height: 100
}}>
Hello
</div>
</div>);
}
const el = document.querySelector("#root");
ReactDOM.render(<X title='Example Component' />, el);
I want to see the div block width increase when the innerWidth become greater than 1000 and decrease when the innerWidth get lower than 1000 BUT I don't want to see the console messages on every listener callback invocation (i.e. every pixel change in the width of the screen).