liveBook · Manning

Mount

As explained in React Quickly, Second Edition

Mounting is the lifecycle phase in which a component is created and its initial rendered representation is inserted into the user interface.

Mounting is the phase when a React component is created and its initial rendered representation is inserted into the user interface. A component can mount when a parent condition includes it, and unmount when that condition removes it. section 6.2.1 section 6.0

Mounting matters because it determines when component effects and resources begin:

  • Run an effect once after mounting by passing an empty dependency array to useEffect. section 6.1.1
  • Start resources such as timers on mount, then return cleanup logic so they stop on unmount. section 6.1.2
  • Avoid repeatedly mounting a data-loading component when toggling visibility, or move its data fetching and storage higher in the component tree. section 6.2.1

A stopwatch that follows the mount lifecycle

function Stopwatch() { const [seconds, setSeconds] = useState(0); useEffect(() => { const interval = setInterval( () => setSeconds((seconds) => seconds + 1), 1000 ); return () => clearInterval(interval); }, []); return <h1>Seconds: {seconds}</h1>; } The empty dependency array starts the interval when Stopwatch mounts. The returned cleanup function clears it when the component unmounts. section 6.1.2

Figure 6.4 To make the effect hook activate with an effect and cleanup only on mount and unmount, respectively, you must add an empty dependency array to make sure that the effect and cleanup never runs just because the component is re-rendering.
06-04

Use the lifecycle diagram to see why an empty dependency array limits the effect and its cleanup to mounting and unmounting rather than ordinary re-renders. section 6.1.2

Mounting also affects asynchronous work. A remote dropdown can load options when it mounts, but if it unmounts before the response arrives, cleanup should cancel the request or prevent a stale state update. section 6.1.1 section 6.1.2

How React Quickly, Second Edition covers it

How deep it goes
Explains conditional mounting, effect dependencies, cleanup, asynchronous cancellation, and class lifecycle methods without detailing React internals.
Examples
Demonstrates remote dropdowns, stopwatches, conditional visibility, event listeners, and class-based lifecycle method order.
What you can do after
Apply mounting via React effects to initialize data, timers, and listeners, then clean them up when components unmount.
How it connects
Connects mounting with unmounting, rendering, re-rendering, effects, cleanup functions, conditional rendering, and component lifecycle methods.

Also explained in

Compare how 3 books explain mount →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.