Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Components and Special Elements

React class lifecycle methods

Reference for React class lifecycle methods: constructor, render, componentDidMount, componentDidUpdate, componentWillUnmount and their hook equivalents.

Class components have methods that React calls at specific moments in a component's life: when it is created and shown (mounting), when its props or state change (updating), and when it is removed (unmounting). You override the ones you need to run code at those moments.

The most used are componentDidMount (start something after the first render), componentDidUpdate (react to a change after it is on screen) and componentWillUnmount (stop what you started). In function components these jobs belong to useEffect.

JSX
class Timer extends Component {
  componentDidMount() { /* start */ }
  componentDidUpdate(prevProps, prevState) { /* react to changes */ }
  componentWillUnmount() { /* clean up */ }
}

Try it

The order of the calls
Start in didMount, stop in willUnmount

The first example prints constructor, render, componentDidMount on mount. A click on the counter adds shouldComponentUpdate, render, getSnapshotBeforeUpdate, componentDidUpdate. Unmounting prints componentWillUnmount. The second one counts 3, 2, 1, 0 and then stops its own timer; if you press the button earlier, componentWillUnmount clears it.

Syntax

The methods in the order React calls them:

Phase Method Purpose
Mount constructor(props) Initialize state and bind methods. Call super(props) first. No side effects.
Mount, update static getDerivedStateFromProps(props, state) Return new state from props, or null. Rarely needed.
Update shouldComponentUpdate(nextProps, nextState) Return false to skip rendering. Prefer PureComponent.
Mount, update render() Return the UI. Pure.
Update getSnapshotBeforeUpdate(prevProps, prevState) Read the DOM (for example scroll position) just before it changes. The returned value is passed to componentDidUpdate.
Mount componentDidMount() The DOM exists. Start subscriptions, timers, requests; measure nodes.
Update componentDidUpdate(prevProps, prevState, snapshot) The DOM was updated. Compare with the previous values before acting.
Unmount componentWillUnmount() Clean up timers, subscriptions, listeners.
Error static getDerivedStateFromError(error), componentDidCatch(error, info) See error boundaries.

Legacy methods componentWillMount, componentWillReceiveProps and componentWillUpdate exist only with the UNSAFE_ prefix and should not be used in new code.

Usage notes

  • Guard componentDidUpdate. It runs after every update. Calling setState there without comparing prevProps or prevState to the current values creates an infinite loop.
  • Mirror what you start. Whatever componentDidMount starts, componentWillUnmount must stop. Forgetting it leaks timers and listeners.
  • Mapping to hooks. componentDidMount + componentWillUnmount correspond to useEffect(() => { ...; return cleanup; }, []). componentDidUpdate with a comparison corresponds to an effect with a dependency array. getSnapshotBeforeUpdate has no hook; keep a class for that rare case.
  • Strict Mode. In development, StrictMode mounts, unmounts and mounts again, so componentDidMount, componentWillUnmount, componentDidMount run in that order once. Code that is properly cleaned up does not notice.
  • No side effects in render or the constructor. React may call them more than once or discard the result.
  • Fetching data. Do it in componentDidMount and guard against unmounting (a flag or an abort controller) so you do not set state afterwards.
  • this binding. Use arrow-function fields for handlers; lifecycle methods are already bound by React.