← Back to React, page 6

react-0102 · Easy fresher

What does one-way data flow mean in React, and how do children send information back to parents?

Data flows down: a parent passes values to children through props, and children cannot change those props. When a child needs to change something, the parent passes down a function, and the child calls it with the new value. The parent updates its own state, and the new value flows down again. This makes it clear where each piece of data is owned and how it changes.

Full answer

In React, props travel from parent to child only. A child receives them as read-only inputs. It cannot assign to a prop, and it has no direct way to reach up and edit the parent's variables. This is called one-way (unidirectional) data flow, or "data down, actions up".

The cycle has four steps. State lives in one owner component. The owner passes values down as props. A child reports an event by calling a callback prop, for example onSelect(id). The owner handles it by updating its state, which re-renders it and gives every child fresh props.

Why it is useful. When something on screen is wrong, you can trace it upward: which component owns this value, and which handler changes it. There is one place to look. Two-way binding systems (as in older AngularJS) let a change in a child update the parent's data directly, which can create loops and makes updates harder to follow. React's controlled inputs are the same cycle: the value comes down, the onChange goes up.

What is not one-way. Context lets a provider send values to any descendant without passing props through each level, but the flow is still downward. Refs and external stores can break the pattern, because they are mutable and read outside of render, so use them deliberately.

Common design question: where should state live? Put it in the closest component that is above everything that needs it (lifting state up).

The example has a parent that owns the selected course and a child list that reports clicks.

jsx
import { useState } from 'react';

function CourseList({ courses, selectedId, onSelect }) {
  return (
    <ul>
      {courses.map((course) => (
        <li key={course.id}>
          <button aria-pressed={course.id === selectedId} onClick={() => onSelect(course.id)}>
            {course.title}
          </button>
        </li>
      ))}
    </ul>
  );
}

export default function CoursePage() {
  const courses = [
    { id: 1, title: 'Maths' },
    { id: 2, title: 'Science' },
  ];
  const [selectedId, setSelectedId] = useState(null);
  const selected = courses.find((course) => course.id === selectedId);

  return (
    <div>
      <CourseList courses={courses} selectedId={selectedId} onSelect={setSelectedId} />
      <p>Selected: {selected ? selected.title : 'nothing'}</p>
    </div>
  );
}

The parent owns selectedId and passes down both the value and the setter as onSelect. The check pressed both buttons in order, so the final state is the second course. Rendered result: <div><ul><li><button aria-pressed="false">Maths</button></li><li><button aria-pressed="true">Science</button></li></ul><p>Selected: Science</p></div>

Key points

  • Props flow from parent to child and are read-only.
  • Children communicate upward by calling callback props.
  • The owner component updates its state and the new values flow down again.
  • One owner per piece of state makes bugs easy to trace.
  • Controlled inputs, and context, follow the same downward flow.

Common mistakes

  • Trying to assign to a prop inside the child.
  • Copying a prop into state and getting out of sync with the parent.
  • Mutating an object received through props.
  • Duplicating the same state in parent and child.
  • Describing controlled inputs as two-way binding.

Follow-ups

  • How does one-way data flow help when debugging?
  • How does context fit into one-way data flow?
  • How do Redux or Zustand keep the flow predictable?

Tips

  • Use the phrase "data down, actions up".
  • Link it to controlled inputs. It shows you see the same pattern.
  • Mention where state should live when two siblings need it.