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)
1. Start the Docker container Start a new Docker ...
Table of contents 1. Variable Overview 1.1 Storag...
Table of contents question Server layer and stora...
Comments and messages were originally a great way...
For a website, usability refers to whether users c...
Preface Recently, I encountered a requirement. Du...
1. Introduction to Varnish Varnish is a high-perf...
Table of contents introduction Why bother? Commun...
Before you begin Have a cloud server, mine is Ten...
This article example shares the specific code of ...
Preface When making a top menu, you will be requi...
Table of contents 1. Download WeChat developer to...
In the horizontal direction, you can set the row ...
As an open source software, Apache is one of the ...
1. About Registry The official Docker hub is a go...