This method is invoked when the component is initially created and when it receives new props. It is used to update the state based on the incoming props.
This method is called after the component has been rendered to the DOM. It is commonly used for tasks like data fetching or setting up event listeners.
Example
componentDidMount() {
// Fetch data or set up event listeners here
}
shouldComponentUpdate
This method determines whether the component should re-render after receiving new props or state. It can be used to optimize rendering by preventing unnecessary updates.
This method is called right before the component's DOM is updated. It allows you to capture some information from the current DOM for use in componentDidUpdate.
This method is called after the component has been updated and re-rendered. It is often used for performing actions that depend on the updated state or props.
Example
componentDidUpdate(prevProps, prevState, snapshot) {
if (snapshot === 'Value changed') {
// Handle the value change
}
}
componentWillUnmount
This method is called just before the component is removed from the DOM. It is used for cleanup tasks like removing event listeners or timers.
Example
componentWillUnmount() {
// Clean up resources here
}