Created
January 28, 2021 19:27
-
-
Save decpk/4e651b92ae4e9a3ee3c20e06105e324a to your computer and use it in GitHub Desktop.
Update phase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from "react"; | |
| import "./styles.css"; | |
| class App extends React.Component { | |
| constructor() { | |
| super(); | |
| this.state = { | |
| counter: 0 | |
| }; | |
| console.log("Inside constructor"); | |
| } | |
| incrementCounter = () => { | |
| this.setState((state) => { | |
| return { | |
| counter: state.counter + 1 | |
| }; | |
| }); | |
| }; | |
| render() { | |
| console.log("Inside render"); | |
| return ( | |
| <div className="App"> | |
| <h1>Component update life cycle</h1> | |
| <h1>{this.state.counter}</h1> | |
| <button onClick={this.incrementCounter}>INCREMENT</button> | |
| </div> | |
| ); | |
| } | |
| componentDidUpdate(oldProps, oldState) { | |
| console.log("Inside componentDidUpdate"); | |
| if (oldState.counter === 3) { | |
| this.setState({ | |
| counter: 0 | |
| }); | |
| } | |
| } | |
| componentDidMount() { | |
| console.log('Inside componentDidMount'); | |
| } | |
| } | |
| export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment