React componentDidUpdate() method


Release date:2024-03-14 Update date:2024-03-15 Editor:admin View counts:121

Label:

React componentDidUpdate() method

The componentDidUpdate() format of the method is as follows:

componentDidUpdate(prevProps, prevState, snapshot)

componentDidUpdate() method is called immediately after the build update.

This method is not performed for the first time rendering.

You can also do it at componentDidUpdate() call directly in the setState() . But note that it must be wrapped in a conditional statement

The following examples use the componentDidUpdate() method is executed after the build is updated, and the build uses the componentDidMount() . The method will be found in the 1 modification operation occurs after a second:

Example

classHeaderextendsReact.Component{constructor(props){super(props);this.state=
{favoritesite:"runoob"};}componentDidMount(){setTimeout(()=>{this.setState
({favoritesite:"google"})},1000)}componentDidUpdate(){document.getElementById("mydiv").
innerHTML="What I like after the update is"+this.state.favoritesite;}render(){return(<div>
<h1>My favorite website is{this.state.favoritesite}</h1> <divid="mydiv"></div>
</div>);}}ReactDOM.render(<Header/>,document.getElementById('root'));

Powered by TorCMS (https://github.com/bukun/TorCMS).