# Flux & Unidirectional Data Flow

> The mental model behind modern state management, introduced by Facebook.

Subject: State Management | Lesson 3 of 22 | 5 min read
Interactive lesson: https://codewithanimation.in/state-management/flux-architecture
Previous: [React Context Performance Trap](https://codewithanimation.in/state-management/context-performance-trap.md) | Next: [Creating a Zustand Store](https://codewithanimation.in/state-management/creating-zustand-store.md)

## What is Flux?

Flux is an architectural pattern that Facebook created to complement React. It enforces a **unidirectional data flow** (one-way data flow), making applications much more predictable than traditional MVC (Model-View-Controller) patterns.

In Flux, data always flows in a single, circular direction: **Action → Dispatcher → Store → View**.

## Why was it invented?

Before Flux, two-way data binding was common. A View could update a Model, which updated another Model, which updated another View. In large applications (like Facebook's unread message counter), this led to cascading updates and impossible-to-trace bugs.

Flux solves this by demanding that Views can never mutate data directly. Instead, Views must dispatch "Actions" (simple objects describing what happened), which are processed centrally.

## The 4 Pillars of Flux

**1. Actions:** Plain JavaScript objects describing an event. E.g., `{ type: 'ADD_TODO', text: 'Buy milk' }`.

**2. Dispatcher:** A central hub that receives all actions and broadcasts them to every registered store.

**3. Stores:** Where the actual state lives. They listen to the dispatcher, update their data based on the action, and then emit a "change" event.

**4. Views:** React components. They listen for "change" events from the store, re-render with the new data, and can trigger new actions based on user input.

## Common mistake: The MVC Chaos

In a traditional MVC app, if a user clicks a button, the Controller updates a Model. That Model's change might trigger another Controller, which updates another Model. Data bounces around unpredictably, making it incredibly hard to answer: *"Why did this button turn green?"*

## Tip: You already use Flux concepts

If you've ever used Redux, Zustand, or even React's `useReducer` hook, you are using the Flux architecture! They all rely on the core concept: Views dispatch Actions, and central Stores update the state.

## Try it: The Flux Loop

Interact with the diagram below to see how a simple click event turns into an Action, travels through the Dispatcher to update the Store, and finally re-renders the View in one predictable loop.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/flux-architecture

## Check yourself

### Question 1

In the Flux architecture, how does a View (React component) update the state?

- A. By calling a setter function directly on the Store.
- B. By dispatching an Action, which is processed by the Store.
- C. By using two-way data binding.

Correct answer: B

To guarantee predictable data flow, Views can only trigger updates by dispatching an Action object into the one-way loop.

- Why not A: Views are never allowed to mutate or set Store data directly.
- Why not C: Two-way data binding is exactly what Flux was designed to replace.

## Remember this

- Flux enforces Unidirectional Data Flow.
- Data flows: Action → Dispatcher → Store → View.
- Views cannot mutate state directly; they must dispatch Actions.
- This pattern eliminates cascading, unpredictable UI updates.
