Frontend / Component lifecycle

Component lifecycle

Follow one weather widget from the moment it appears on the page to the moment it is removed: when it renders, when it may fetch and subscribe, and what it must clean up.

Beginner7 steps~7 min
Component — the Weather widget, on the page for a while
Phase — where in its lifetime the component is
Outside system — a server or timer the component talks to
// THE SHORT VERSION

In 30 seconds

Every component has a lifetime: it is mounted (created and put on the page), updated whenever its props or state change, and finally unmounted (removed).

Rendering only works out what to show. Fetching data and subscribing to timers or sockets happen after the component is on the page, and whatever was started must be stopped again in a cleanup.

Class components use three methods for this: componentDidMount, componentDidUpdate and componentWillUnmount. With hooks, a single useEffect and its cleanup function cover all three.

Component lifecycle, step by step

STEPS01 / 07

load() is a made-up helper that fetches and calls setTemp. In development, React Strict Mode mounts each component twice to expose missing cleanups. Many apps fetch through a framework or data library instead.

STEP 01 / 07 A component has a lifetime
THE MATH
mount: once update: any number of times unmount: once
  1. Step 1: A component has a lifetime. A component is not on the page forever. React creates it and inserts it (mount), redraws it whenever its props or state change (update), and finally removes it (unmount). Mounting and unmounting happen once each; updates can happen any number of times in between.
  2. Step 2: Mount: the first render. Mounting starts with the first render: React calls Weather with city = 'Oslo', gets back a description of the widget and creates the page elements for it. No forecast is known yet, so it shows a placeholder. Rendering must only compute what to show; it is not the place to contact a server or start a timer.
  3. Step 3: After mount: fetch, subscribe. Once the widget is on the page, React runs the component's effect. This is the place for outside work: request the forecast for Oslo, and subscribe to a timer that will refresh it every 60 seconds. In a class component the same code goes in componentDidMount.
  4. Step 4: Update: state changed. The forecast arrives and the code stores it with setTemp(4). A state change starts an update: React calls Weather again, compares the result with what is on the page and changes only the text that differs. The timer is left alone: it was set up for Oslo, and Oslo is still the city.
  5. Step 5: Update: a prop changed. The parent now passes city = 'Lima'. This is also an update, but the outside work was set up for Oslo and is out of date. React first runs the cleanup, which stops the Oslo timer, then runs the effect again for Lima: a new request and a new timer. A class does both by hand in componentDidUpdate.
  6. Step 6: Unmount: clean up. The user leaves the page and React removes the widget. As part of removing it, React runs the cleanup one last time, which stops the Lima timer; a request still in flight should be cancelled or ignored too. Without cleanup the timer would keep firing and trying to update a component that no longer exists: a leak.
  7. Step 7: Hooks and class methods. Class components split this work across three methods named after moments in the lifetime. A hook groups it by task instead: one useEffect holds the set-up, the function it returns is the cleanup, and the dependency list says when to redo both. Same lifecycle, written once.
VALUES SO FAR
life3 phasesorder
city····prop
temp····state
fetch····effect
timer····subscribe
temp····state
city····prop
cleanup····unmount
hooks····mapping

Spotted a mistake on this page?