Skip to content

Instantly share code, notes, and snippets.

@ninadvadujkar
Last active August 7, 2020 06:00
Show Gist options
  • Save ninadvadujkar/9a00fae92448f946279857154a31e868 to your computer and use it in GitHub Desktop.
Save ninadvadujkar/9a00fae92448f946279857154a31e868 to your computer and use it in GitHub Desktop.
Phases
1. Initialisation
2. Mounting
3. Updating
4. Unmounting
1. Initialisation
Setting up of initial state and default props happen here.
a. constructor
- Constructor is called during initialisation.
2. Mounting
a. componentWillMount
- Executed just before the React component is about to mount in the DOM. Executed once before 1st render
b. componentDidMount
- Executed after the component is mounted in the DOM.
NOTE: API calls should be made in componentDidMount
3. Updating
a. shouldComponentUpdate
- Method should return `true` or `false` and if `true` the component's render method is called. Not required most of the times unless we're dealing with a heavy component that can have performance bottlenecks.
- React.PureComponent always does a shallow copy before deciding whether component should be updated
b. componentWillUpdate
- Executed only after `shouldComponentUpdate` returns `true`. Used to prepare for upcoming render.
c. componentDidUpdate
- Executed after the new update is rendered in the DOM.
Below methods are called when parent sends new props:
a. componentWillReceiveProps
- Gets executed when props have changed and it's not the first render.
4. Unmounting
a. componentWillUnmount
- Last method in the lifecycle. Executed just before component is removed from the DOM. Used mainly for cleanup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment