React Foundations ยท State

Lifting state up

Lifting state up

Two components need to show the same number. You give each one its own copy of the state, and soon they disagree: one says 3, the other still says 2.

The fix is to move the state up to the closest common parent. The parent owns the value and passes it down to each child as a prop:

function Display({ value }) {
  return <p>{value}</p>;
}

function App() {
  const [count, setCount] = useState(0);
  return <Display value={count} />;
}

Display is now a presentational component: it holds no state of its own and renders whatever value it is given. App is the single source of truth. When App's state changes, it re-renders and passes the new value down, so every child that reads the prop stays in sync.

This is called lifting state up. It keeps one copy of the data, so the components can never disagree.

Your job is to lift a count into App and pass it to a child in App.jsx.

Challenge

A child shows a count, but the button that should change it sits elsewhere and nothing connects them. Export an App component from App.jsx that holds a count in state, renders a child component showing that count in a paragraph, and renders a button labelled Increment that adds one.

The Test clicks the button and expects the paragraph to read 1. The starter renders a fixed 0 and never updates it, so the Test fails.

App.jsx
export function App() {
  return (
    <div>
      <p>0</p>
      <button>Increment</button>
    </div>
  );
}

No test run yet

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