A React class component goes through three phases: Mounting (created and inserted into the DOM), Updating (re-rendered when props or state change) and Unmounting (removed from the DOM). React calls specific lifecycle methods in each phase.
Diagram (in order of calls):
MOUNTING UPDATING UNMOUNTING
constructor() getDerivedStateFromProps() componentWillUnmount()
getDerivedState- shouldComponentUpdate()
FromProps() render()
render() getSnapshotBeforeUpdate()
componentDidMount() componentDidUpdate()
1. Mounting
constructor(): initialise state, bind methods.
getDerivedStateFromProps(): set state from props before rendering (rare).
render(): returns the JSX.
componentDidMount(): runs after the component is in the DOM; used for API calls, timers.
2. Updating (on new props, setState() or forceUpdate())
shouldComponentUpdate(): return false to skip a re-render.
render().
getSnapshotBeforeUpdate(): read DOM values (e.g. scroll position) just before the update.
componentDidUpdate(): runs after the update; can compare old and new props/state.
3. Unmounting
componentWillUnmount(): clean up timers, subscriptions and listeners.
In function components the same work is done with the useEffect Hook: the effect runs after mounting/updating, and its returned cleanup function runs before unmounting.