JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext

Learn the concept

Unidirectional Data Flow

react
junior
data-flow

What is unidirectional data flow in React and why is it important?

data-flow
unidirectional
props
state
one-way-binding
Quick Answer

Unidirectional data flow means data moves one direction only: parent → child via props. Children cannot modify parent state directly; they must use callback functions. This makes state changes predictable and debugging easier.

Detailed Explanation

What is Unidirectional Data Flow:

  • Data flows in one direction: parent → child via props
  • Child components cannot modify received props (read-only)
  • To update parent state, children call callback functions passed as props
  • Also called 'one-way data binding'

Key Principles:

  1. Props are Immutable: Child components receive props but cannot change them

  2. State Ownership: Each piece of state is owned by one component; changes only affect its children

  3. Callbacks for Updates: Children communicate upward by calling functions passed as props

  4. Single Source of Truth: Data originates from one place, making state predictable

Why It Matters:

  • Predictable: You always know where data comes from
  • Debuggable: Easy to trace data flow and find issues
  • Maintainable: Clear data ownership prevents spaghetti code

Contrast with Two-Way Binding:

  • Vue's v-model is syntactic sugar over one-way data flow (prop + event) — still unidirectional underneath
  • Angular's [(ngModel)] provides true two-way binding, but modern Angular favors signals
  • React makes the data flow explicit, which many teams find easier to debug in large codebases

React 19 Data Flow Extensions:

  • Server Components introduce server-to-client data flow (data fetched on server, streamed to client)
  • Form Actions (useActionState) provide a new pattern for client-to-server data submission

Code Examples

Unidirectional flow: Parent → Child → CallbackJSX
// Parent component owns the state
function ShoppingCart() {
  const [items, setItems] = useState([]);
  const [total, setTotal] = useState(0);

  // Callback passed to child for upward communication
  const addItem = (product) => {
    setItems([...items, product]);
    setTotal(total + product.price);
  };

  const removeItem = (id) => {
    const item = items.find(i => i.id === id);
    setItems(items.filter(i => i.id !== id));
    setTotal(total - item.price);
  };

  return (
    <div>
      {/* Data flows DOWN via props */}
      <ProductList onAddItem={addItem} />
      
      {/* State passed down, callback passed for removal */}
      <CartItems 
        items={items}           // Data flows down
        onRemove={removeItem}   // Callback for upward communication
      />
      
      <CartTotal total={total} />
    </div>
  );
}

Real-World Applications

Use Cases

Debugging Production Issues

Tracing data flow from source to UI when investigating bugs in production

State Architecture Design

Deciding where to place state in the component tree using lifting state principles

Form Data Propagation

Managing form field values flowing down and change events flowing up through component hierarchies

Mini Projects

Data Flow Visualizer

beginner

Build an app that visually shows props flowing down and callbacks flowing up through a component tree

State Lifting Exercise

intermediate

Refactor a multi-component form to properly lift shared state to the nearest common ancestor

Industry Examples

Redux

Built entirely on unidirectional data flow principle (action -> reducer -> store -> view)

Flux Architecture

Facebook's original architecture pattern that inspired React's data flow model

Resources

React Docs - Sharing State Between Components

docs

Unidirectional Data Flow in React - GeeksforGeeks

article

Related Questions

What are props in React and how do you pass data between components?

junior
props

What is React Context and when should you use it?

mid
context
Previous
Why would you choose React over other frameworks like Vue or Angular?
Next
What is the Virtual DOM and how does it work?
PrevNext