RSRobince Studio

Lifting State Up

When two components need the same data, move that state up into their closest shared parent. This is called lifting state up.

The parent owns the state and passes it down as props. Children ask the parent to change it by calling a function passed down as a prop.

  • State lives in the common parent
  • Values flow down through props
  • Changes flow up through callback props
function Parent() {
  const [value, setValue] = React.useState(0);
  return <Child value={value} onAdd={() => setValue(value + 1)} />;
}
Keep state as low as possible, but high enough that every component needing it can reach it.
Try it yourself

Loading editor…