2018-04-06 14:40:48 -04:00
|
|
|
# React-Lifecycle-Cheatsheet
|
|
|
|
|
2018-04-06 14:50:21 -04:00
|
|
|
Short reference of React.Component lifecycle methods so I don't have to scroll through the huge text on the official documentation
|
|
|
|
|
|
|
|
![Lifecycle Diagram](./Lifecycle-chart.jpg)
|
|
|
|
|
|
|
|
## Mounting
|
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
#### constructor ()
|
2018-04-06 14:50:21 -04:00
|
|
|
|
2018-04-06 15:02:31 -04:00
|
|
|
```js
|
2018-04-06 14:50:21 -04:00
|
|
|
/**
|
|
|
|
* Create state and call super
|
|
|
|
*
|
|
|
|
* @param {object} props
|
|
|
|
*/
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
foo: 'bar'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
#### static getDerivedStateFromProps ()
|
|
|
|
|
2018-04-06 15:02:31 -04:00
|
|
|
```js
|
2018-04-06 15:01:51 -04:00
|
|
|
/**
|
|
|
|
* Props have changed, return the updated state
|
|
|
|
*
|
|
|
|
* @param {object} nextProps - Props for the next render
|
|
|
|
* @param {object} prevState - State from previous render
|
|
|
|
* @return {object | null} - The change to the state
|
|
|
|
*/
|
|
|
|
static getDerivedStateFromProps (nextProps, prevState) {
|
|
|
|
// Nothing to update
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// Set new state
|
|
|
|
return {
|
|
|
|
foo: 'foobar'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### componentDidMount ()
|
2018-04-06 14:50:21 -04:00
|
|
|
|
|
|
|
## Updating
|
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
#### shouldComponentUpdate ()
|
|
|
|
#### render ()
|
|
|
|
#### getSnapshotBeforeUpdate ()
|
|
|
|
#### componentDidUpdate ()
|
2018-04-06 14:50:21 -04:00
|
|
|
|
|
|
|
## Unmounting
|
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
#### componentWillUnmount ()
|
|
|
|
|
|
|
|
---
|
|
|
|
## Deprecated
|
|
|
|
|
|
|
|
### UNSAFE_componentWillMount ()
|
|
|
|
### UNSAFE_componentWillReceiveProps ()
|
|
|
|
### UNSAFE_componentWillUpdate ()
|