Compared with the old life cycle
After React16, three lifecycles are deprecated (but not deleted)
New hooks added to the lifecycle of the new version
getSnapshotBeforeUpdate
getDerivedStateFromPropsgetDerivedStateFromProps is not for instances, it needs to be defined as a static method. And need to give a return value The return value can be state Obj or null //Get a derived state from props static getDerivedStateFromProps(props,state){ return props } If the value of state depends on props at all times, you can use getDerivedStateFromProps <div id="test"></div> <!-- Import react core library --> <script src="../js/17.0.1/react.development.js"></script> <!-- Introduce react-dom to support react to operate dom --> <script src="../js/17.0.1/react-dom.development.js"></script> <!-- Introduce babel to convert jsx to js --> <script src="../js/17.0.1/babel.min.js"></script> <script type='text/babel'> // Create component class Count extends React.Component{ // Constructor constructor(props){ console.log('Count ---constructor') super(props) // Initialize state this.state = {count:0} } //Hook componentDidMount(){ console.log('Count --- componentDidMount') } // Uninstall component button callback death=()=>{ ReactDOM.unmountComponentAtNode(document.getElementById('test')) } // Implement +1 add =()=>{ // Get the original state const {count} = this.state // Update status this.setState({count:count+1}) } // Callback for the forced update button force=()=>{ this.forceUpdate() } static getDerivedStateFromProps(props,state){ console.log('getDerivedStateFromProps', props, state) return props } // Control component update valve shouldComponentUpdate(){ console.log('Count --- shouldComponentUpdate') // If the return value is false, the valve is closed by default. return true } //Component update completed hook componentDidUpdate(){ console.log('Count --- componentDidUpdate') } // The component will be uninstalled hook componentWillUnmount(){ console.log('Count --- componentWillUnmount'); } render(){ console.log('Count --- render') const {count} = this.state return( <div> <h2>Current sum: {count}</h2> <button onClick={this.add}>Click me +1</button> <button onClick={this.death}>Click me to uninstall the component</button> <button onClick={this.force}>Click here to force update (without changing data)</button> </div> ) } } // Rendering component ReactDOM.render(<Count count={166}/>,document.getElementById('test')) </script> Execution Results getSnapshotBeforeUpdate The return value can be null or a snapshot. If it is null, it has no effect. If it is a snapshot, the return value can be passed to the third parameter of componentDidUpdate. <div id="test"></div> <!-- Import react core library --> <script src="../js/17.0.1/react.development.js"></script> <!-- Introduce react-dom to support react to operate dom --> <script src="../js/17.0.1/react-dom.development.js"></script> <!-- Introduce babel to convert jsx to js --> <script src="../js/17.0.1/babel.min.js"></script> <script type='text/babel'> // Create component class Count extends React.Component{ // Constructor constructor(props){ console.log('Count ---constructor') super(props) // Initialize state this.state = {count:0} } //Hook componentDidMount(){ console.log('Count --- componentDidMount') } // Uninstall component button callback death=()=>{ ReactDOM.unmountComponentAtNode(document.getElementById('test')) } // Implement +1 add =()=>{ // Get the original state const {count} = this.state // Update status this.setState({count:count+1}) } // Callback for the forced update button force=()=>{ this.forceUpdate() } static getDerivedStateFromProps(props,state){ console.log('getDerivedStateFromProps', props, state) return null } getSnapshotBeforeUpdate(){ console.log('getSnapshotBeforeUpdate'); return "eee" } // Control component update valve shouldComponentUpdate(){ console.log('Count --- shouldComponentUpdate') // If the return value is false, the valve is closed by default. return true } //Component update completed hook componentDidUpdate(preProps,preState,snapshotValue){ console.log('Count---1componentDidUpdate',preProps,preState,snapshotValue); } // The component will be uninstalled hook componentWillUnmount(){ console.log('Count --- componentWillUnmount'); } render(){ console.log('Count --- render') const {count} = this.state return( <div> <h2>Current sum: {count}</h2> <button onClick={this.add}>Click me +1</button> <button onClick={this.death}>Click me to uninstall the component</button> <button onClick={this.force}>Click here to force update (without changing data)</button> </div> ) } } // Rendering component ReactDOM.render(<Count count={166}/>,document.getElementById('test')) </script> SummarizeThree Phases of the Life Cycle (New)1. Initialization phase: triggered by ReactDOM.render() - initial rendering constructor()getDerivedStateFromPropsrender()componentDidMount() 2. Update phase: triggered by this.setSate() inside the component or re-rendering of the parent component getDerivedStateFromPropsshouldComponentUpdate()render()getSnapshotBeforUpdatecomponentDidUpdate() 3. Unmounting components: triggered by ReactDOM.unmountComponentAtNode() componentWillUnmount() Important hookrender: initialize rendering or update rendering call componentDidMount: start monitoring, send ajax request componentWillUnmount: do some finishing work, such as: clean up timer The hook that will be abandoned
A warning will appear when using it now. The next major version will require the UNSAFE_ prefix to be used. It may be completely abandoned in the future and is not recommended. This is the end of this article about the new version of react lifecycle hook function. For more relevant react lifecycle hook function content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Tutorial on processing static resources in Tomcat
>>: Using keras to judge SQL injection attacks (example explanation)
Table of contents 01 sql_slave_skip_counter param...
This article example shares the specific code of ...
Everyone knows that images on web pages are genera...
Preface In daily code development, there are many...
Declare the parameter name, type and permission y...
1. Prepare data The following operations will be ...
Frequently asked questions Access denied for user...
This article shares the MySQL free installation c...
The optimization created by MySQL is to add index...
Table of contents Case 1: Case 2: Case 3: To summ...
Installation path: /application/mysql-5.7.18 1. P...
Phenomenon Start the Docker container docker run ...
Table of contents 1. Effect Demonstration 2. Impl...
Copy code The code is as follows: html, address, ...
Today's web designs tend to display very larg...