useEffect lifecycle
Follow one chat component through three renders to see when React runs an effect, when it skips it, when it cleans up, and how the dependency array decides.
In 30 seconds
In React, rendering only works out what the screen should show. Work that reaches outside React, such as opening a connection, goes into an effect, which runs after the screen has been updated.
The dependency array lists the values the effect uses. React runs the effect again only when one of them has changed, and first runs the cleanup function returned by the previous run.
An empty array means once; no array means after every render. If a value can be computed from props and state, it does not need an effect at all.
useEffect lifecycle, step by step
connect() is a made-up helper. In development, Strict Mode runs each new effect as setup, cleanup, setup to expose missing cleanups; that extra cycle is not drawn here.
- Step 1: Rendering only calculates. React calls the Chat function to work out what the screen should show, then updates the screen to match. That call may happen often and must do nothing else. Opening a connection to a chat server is a side effect: it reaches outside React, so it needs a different place to live.
- Step 2: Effects run after render. useEffect hands React a function to run later. React renders, updates the screen, and only then runs the effect, so outside work does not hold up what the user sees. Here the effect connects to the room general.
- Step 3: The dependency array. The array after the function lists the values the effect uses. After each render React compares every item with its value from the previous render. Chat rendered again, but roomId is still general, so the effect is skipped and the connection stays as it is.
- Step 4: Cleanup comes first. The function an effect returns is its cleanup. When roomId changes to travel, React renders, updates the screen, runs the old cleanup to leave general, then runs the effect again to join travel. When Chat is removed from the screen, the last cleanup runs once more.
- Step 5: Empty array or no array. Take four renders in a row: general, general, travel, travel. With no array the effect runs after all four. With an empty array nothing can ever differ, so it runs after the first render only. With [roomId] it runs after the first render and again when the room changes.
- Step 6: The endless loop. Setting state causes a render. If an effect sets state every time it runs, and it runs after every render, the two chase each other without end. Adding [count] does not help, because the effect changes count itself. Objects and arrays created during render are new every time, so they retrigger an effect as well.
- Step 7: Often no effect is needed. An effect exists to keep a component in step with something outside React: a connection, a timer, a browser API. A value that can be worked out from props and state should simply be computed during render; putting it in an effect adds a second render and a place for bugs. Work caused by a click belongs in the event handler.
Try it yourself
Learn this first
Spotted a mistake on this page?