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 20:35:49 -04:00
|
|
|
`constructor () => void`
|
|
|
|
|
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) {
|
2018-04-06 20:35:49 -04:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
foo: 'bar'
|
|
|
|
}
|
2018-04-06 14:50:21 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
#### static getDerivedStateFromProps ()
|
|
|
|
|
2018-04-06 20:35:49 -04:00
|
|
|
`static getDerivedStateFromProps (nextProps, prevState) => nextState`
|
|
|
|
|
2018-04-06 15:02:31 -04:00
|
|
|
```js
|
2018-04-06 15:01:51 -04:00
|
|
|
/**
|
2018-04-06 20:35:49 -04:00
|
|
|
* Props have changed, return the updated state or null
|
2018-04-06 15:01:51 -04:00
|
|
|
*
|
|
|
|
* @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) {
|
2018-04-06 20:35:49 -04:00
|
|
|
// Nothing to update
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// Set new state
|
|
|
|
return {
|
|
|
|
foo: 'foobar'
|
|
|
|
};
|
2018-04-06 15:01:51 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### componentDidMount ()
|
2018-04-06 14:50:21 -04:00
|
|
|
|
2018-04-06 20:35:49 -04:00
|
|
|
`componentDidMount() => void`
|
|
|
|
|
2018-04-06 15:13:49 -04:00
|
|
|
```js
|
|
|
|
/**
|
|
|
|
* Component mounted, will render soon
|
|
|
|
*/
|
|
|
|
componentDidMount () {
|
2018-04-06 20:35:49 -04:00
|
|
|
// Network calls, state changes,
|
|
|
|
// anything is fair game here
|
2018-04-06 15:13:49 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-04-06 14:50:21 -04:00
|
|
|
## Updating
|
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
#### shouldComponentUpdate ()
|
2018-04-06 15:13:49 -04:00
|
|
|
|
2018-04-06 20:35:49 -04:00
|
|
|
`shouldComponentUpdate (nextProps, nextState) => shouldUpdate (boolean)`
|
|
|
|
|
2018-04-06 15:13:49 -04:00
|
|
|
```js
|
|
|
|
/**
|
|
|
|
* Hook to control re-render
|
|
|
|
*
|
|
|
|
* @param {object} nextProps
|
|
|
|
* @param {object} nextState
|
|
|
|
* @return {boolean} - Whether to render this cycle
|
|
|
|
*/
|
|
|
|
shouldComponentUpdate (nextProps, nextState) {
|
2018-04-06 20:35:49 -04:00
|
|
|
// Default in React.Component
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// React.PureComponent implements this method
|
|
|
|
// with a shallow compare of props and state
|
2018-04-06 15:13:49 -04:00
|
|
|
}
|
|
|
|
```
|
2018-04-06 20:35:49 -04:00
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
#### render ()
|
2018-04-06 15:13:49 -04:00
|
|
|
|
2018-04-06 20:35:49 -04:00
|
|
|
`render () => JSX`
|
|
|
|
|
2018-04-06 15:13:49 -04:00
|
|
|
```js
|
|
|
|
/**
|
|
|
|
* Render returned components
|
|
|
|
*
|
|
|
|
* @return {React Element | string | number | Portal | null | boolean}
|
|
|
|
*/
|
|
|
|
render () {
|
2018-04-06 20:35:49 -04:00
|
|
|
return <div />;
|
2018-04-06 15:13:49 -04:00
|
|
|
}
|
|
|
|
```
|
2018-04-06 20:35:49 -04:00
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
#### getSnapshotBeforeUpdate ()
|
2018-04-06 15:13:49 -04:00
|
|
|
|
2018-04-06 20:35:49 -04:00
|
|
|
`getSnapshotBeforeUpdate (prevProps, prevState) => any`
|
|
|
|
|
2018-04-06 15:13:49 -04:00
|
|
|
```js
|
2018-04-06 20:35:49 -04:00
|
|
|
/**
|
|
|
|
* Get/set DOM values. Return value is passed to componentDidUpdate
|
|
|
|
*
|
|
|
|
* @param {object} prevProps
|
|
|
|
* @param {object} prevState
|
|
|
|
* @return {any}
|
|
|
|
*/
|
2018-04-06 15:13:49 -04:00
|
|
|
getSnapshotBeforeUpdate (prevProps, prevState) {
|
2018-04-06 20:35:49 -04:00
|
|
|
return {
|
|
|
|
bar: 'foo'
|
|
|
|
};
|
2018-04-06 15:13:49 -04:00
|
|
|
}
|
|
|
|
```
|
2018-04-06 20:35:49 -04:00
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
#### componentDidUpdate ()
|
2018-04-06 14:50:21 -04:00
|
|
|
|
2018-04-06 20:35:49 -04:00
|
|
|
`componentDidUpdate (prevProps, prevState, snapshot) => void`
|
|
|
|
|
2018-04-06 15:13:49 -04:00
|
|
|
```js
|
2018-04-06 20:35:49 -04:00
|
|
|
/**
|
|
|
|
* The component updated, do stuff before the next cycle, like network requests
|
|
|
|
*
|
|
|
|
* @param {object} prevProps
|
|
|
|
* @param {object} prevState
|
|
|
|
* @param {any | undefined} snapshot - output from getSnapshotBeforeUpdate
|
|
|
|
*/
|
2018-04-06 15:13:49 -04:00
|
|
|
componentDidUpdate (prevProps, prevState, snapshot = undefined) {
|
2018-04-06 20:35:49 -04:00
|
|
|
// Best place to do actions depending on previous props and state
|
2018-04-06 15:13:49 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-04-06 14:50:21 -04:00
|
|
|
## Unmounting
|
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
#### componentWillUnmount ()
|
|
|
|
|
2018-04-06 20:35:49 -04:00
|
|
|
`componentWillUnmount () => void`
|
|
|
|
|
2018-04-06 15:13:49 -04:00
|
|
|
```js
|
2018-04-06 20:35:49 -04:00
|
|
|
/**
|
|
|
|
* Teardown stuff
|
|
|
|
*/
|
2018-04-06 15:13:49 -04:00
|
|
|
componentWillUnMount () {
|
2018-04-06 20:35:49 -04:00
|
|
|
// Cleanup whatever you need to before the
|
|
|
|
// component unmounts
|
2018-04-06 15:13:49 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
---
|
|
|
|
## Deprecated
|
|
|
|
|
|
|
|
### UNSAFE_componentWillMount ()
|
2018-04-06 20:35:49 -04:00
|
|
|
|
|
|
|
`componentWillMount () => void`
|
|
|
|
|
|
|
|
Use [`componentDidMount ()`](#componentdidmount-) instead.
|
|
|
|
|
2018-04-06 15:01:51 -04:00
|
|
|
### UNSAFE_componentWillReceiveProps ()
|
2018-04-06 20:35:49 -04:00
|
|
|
|
|
|
|
`componentWillRecieveProps (newProps) => void`
|
|
|
|
|
|
|
|
Use [`static getDerivedStateFromProps ()`](#static-getderivedstatefromprops-) instead.
|
|
|
|
|
|
|
|
### UNSAFE_componentWillUpdate ()
|
|
|
|
|
|
|
|
`componentWillUpdate (nextProps, nextState) => void`
|
|
|
|
|
|
|
|
Use [`componentDidUpdate ()`](#componentdidupdate-) instead.
|