React Foundations · Effects & Refs

Running an effect after render

useEffect after render

Running an effect after render

A component should only compute its JSX. That keeps rendering predictable: same props, same output. But plenty of work does not fit that rule — setting the page title, opening a connection, starting a timer. Those touch the world outside React, and React calls them side effects.

Reach for useEffect when you need one:

useEffect(() => {
  document.title = title;
}, [title]);

React runs the effect after it has rendered and committed the DOM. So the screen is already correct before your code touches it.

The second argument is the dependency array. React watches those values and re-runs the effect only when one changes. With [title], the title is set on the first render and again whenever title changes. An empty array ([]) means "run once, after the first render". Leave the array off and the effect runs after every render.

Your job is to set the document title from a prop in App.jsx.

Challenge

The heading shows a title, but the browser tab still shows the old one. Export an App component from App.jsx that takes a title prop, renders it in a level-one heading, and sets document.title to it inside an effect.

The Test renders your component with title="Hello" and expects document.title to be Hello. The starter renders the heading but never sets the title, so the Test fails.

App.jsx
export function App({ title }) {
  return <h1>{title}</h1>;
}

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.