How to re-render the view when the browser is resized in react.js?

Answer

We can listen to the resize event in componentDidMount() and then update the dimensions (width and height).
We should remove the listener in componentWillUnmount() method.

class WindowDimensions extends React.Component {
constructor(props){
super(props);
this.updateDimensions = this.updateDimensions.bind(this);
}

componentWillMount() {
this.updateDimensions()
}

componentDidMount() {
window.addEventListener('resize', this.updateDimensions)
}

componentWillUnmount() {
window.removeEventListener('resize', this.updateDimensions)
}

All react js Questions

Ask your interview questions on react-js

Write Your comment or Questions if you want the answers on react-js from react-js Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---