Flux in React.js
Flux is an application architecture or design pattern for creating client-side web applications. It uses a unidirectional data flow to supplement React.js composable view components. Facebook introduced it to help manage data exchange across components in large apps and to give JavaScript applications a predictable state container. React may leverage unidirectional flow with the help of the straightforward Flux in React.js design. We’ve already talked about how React works well with unidirectional flow. When data is altered, React adheres to the render-always model. The opposite of two-way bindings, data does not flow in the other direction.
Flux is an approach to managing application state and one-way logic, guaranteeing that data is kept whereas React.js is a framework that solely concentrates on an application’s view layer. This one-way flow simplifies data administration and facilitates reasoning about data changes over time, particularly when an application grows in size. Flux in React.js uses a unidirectional data flow instead of MVC.
Whenever a user interacts with a React view, the view changes all of the impacted views by propagating an action to the different stores that contain the application’s data and business logic via a central dispatcher. The declarative programming language of React, which enables the store to deliver updates without defining how to transition views between states, makes this particularly effective.
Core Concepts of Flux in React.js
The conventional Model-View-Controller (MVC) designs are not followed by Flux in React.js. Rather, it is composed on four primary components: A Dispatcher, Stores, Views, and Actions (React Components). The dispatcher, storage, and views (React components) make up flux applications. These are not Model View Controller. Flux in React.js applications include controller-views at the top of the hierarchy that retrieve data from stores and transmit it down to their children. Action creators dispatcher helper methods support a semantic API that defines all application changes. Consider them a fourth part of the Flux update cycle.

Actions: As descriptive payloads, actions are straightforward objects that provide the type of action and any additional pertinent input data. The helper methods known as “action creators,” which offer a semantic API for any potential modification in the application, are responsible for creating actions.
Dispatcher: The application’s dispatcher serves as its main hub, accepting all activities and forwarding them to the stores that have registered. Payload forwarding is its main function; it is a single action receiver and callbacks controller that does not carry out sophisticated logic or data processing.
Stores: The logic and state of the application are stored in stores. Stores notify the dispatcher of callbacks and, depending on the action type received, determine whether to take any action. Since Redux frequently does this behind the scenes with libraries like Immer, they are in charge of state manipulation, which involves constructing and returning a new state without really changing the old one in pure Flux in React.js. A store notifies views by generating an event after making modifications.
Views (React Components): The React components that render the user interface are called views (React components). A modification to the data layer causes views to re-render themselves after receiving data from the stores. Additionally, views can generate actions and send them to the dispatcher in reaction to user input. The “controller-views” at the top of the component hierarchy are frequently responsible for retrieving data from stores and transferring it to their offspring.
Before being displayed on the screen, data enters the application and moves through it in a single path with this unidirectional data flow. All impacted views are updated when a user interacts with a React view, which subsequently sends an action to the stores via the central dispatcher. The declarative programming language of React makes this possible, enabling the store to transmit updates without having to define how to move views between states.
Flux vs. Redux
Many Flux in React.js concepts are followed by Redux, a well-known state management package that is sometimes regarded as a Flux-based implementation. Notable distinctions include:
Single State Tree: Redux keeps an unchangeable state tree for the entire program. A Single State Tree is crucial to React state management, especially in designs like Redux, where the entire application’s state is kept in a single JavaScript object. This design approach assures a single for all data, which means one central component or store manages and distributes the application’s data.
No Separate Dispatcher:The Dispatcher receives actions and sends them to all registered stores in Flux in React.js architecture. Redux without a Dispatcher breaks the Flux pattern. Instead, Redux controls the entire application with one state tree. Components provide actions to the Redux store to update data. Redux sends dispatched actions to reducers, which calculate and return a new state.
Reducers: Redux “reducers” define how actions alter the state. These pure functions return a new state after receiving an action and the existing state. Modern Redux (with Redux Toolkit) simplifies code by “mutating” state directly in reducers with Immer while keeping core immutability.
Boilerplate: The officially supported Redux Toolkit helps minimise boilerplate code, which can be a significant part of vanilla Redux. It offers tools to quickly generate actions and reducers as well as configure the store.
Code Example:
Using an application called “Social Media Tracker” that seeks to display user data from Reddit and Twitter, let’s demonstrate the Flux design.
import React, { useState, useEffect } from "react";
const Dispatcher = { _cbs: [], reg: cb => Dispatcher._cbs.push(cb), dis: p => Dispatcher._cbs.forEach(cb => cb(p)) };
const Store = {
_state: { twitter: { name: "Gemini", followers: 1000 }, reddit: { name: "GeminiOfficial", karma: 5000 } },
_ls: [],
get: () => Store._state,
on: l => Store._ls.push(l),
off: l => Store._ls = Store._ls.filter(x => x !== l),
handle: ({ type, data }) => {
Store._state = { ...Store._state, [type.toLowerCase()]: data };
Store._ls.forEach(l => l());
}
};
Dispatcher.reg(Store.handle);
const Actions = {
twitter: d => Dispatcher.dis({ type: "TWITTER", data: d }),
reddit: d => Dispatcher.dis({ type: "REDDIT", data: d })
};
const Tracker = ({ t, r }) => {
const [f, setF] = useState(""), [k, setK] = useState("");
return (
<div className="flex flex-col md:flex-row gap-6">
{[
{ label: "Twitter", data: t, color: "blue", val: f, set: setF, act: () => Actions.twitter({ name: "Gemini", followers: +f || 0 }) },
{ label: "Reddit", data: r, color: "red", val: k, set: setK, act: () => Actions.reddit({ name: "GeminiOfficial", karma: +k || 0 }) }
].map(({ label, data, color, val, set, act }) => (
<div key={label} className="flex-1 space-y-4">
<div className="p-4 bg-white rounded-lg shadow-md border">
<h3 className={`text-lg font-semibold text- {color}-600`}>{label}</h3>
<p>Name: {data.name}</p>
<p>{data.followers ?? data.karma} {label === "Twitter" ? "followers" : "karma"}</p>
</div>
<div className="flex gap-2 p-4 bg-gray-50 rounded-lg shadow-md border">
<input type="number" placeholder={`{label} {label === "Twitter" ? "followers" : "karma"}`} value={val} onChange={e => set(e.target.value)}
className="flex-grow p-2 border rounded-md" />
<button onClick={act} className={`bg- {color}-600 text-white px-4 py-2 rounded-md`}>Update</button>
</div>
</div>
))}
</div>
);
};
export default function App() {
const [s, setS] = useState(Store.get());
useEffect(() => { const u = () => setS(Store.get()); Store.on(u); return () => Store.off(u); }, []);
return (
<div className="min-h-screen bg-gray-100 p-8">
<h1 className="text-3xl font-bold text-center mb-8">Social Media Tracker (Flux)</h1>
<Tracker t={s.twitter} r={s.reddit} />
</div>
);
}
Output:
Social Media Tracker (Flux)
Twitter
Name: Gemini
1000 followers
Twitter followers Update
Reddit
Name: GeminiOfficial
5000 karma
Reddit karma Update
Benefits of Flux
Flux has various benefits for creating intricate web apps. Due to limited structures and a small amount of additional code to facilitate state changes at runtime, the Flux in React.js approach is highly helpful when your ReactJS frontend application is intended to evolve.
Single-Directional Data Flow: Since updates always go the same way, this simplifies debugging and understanding the application’s data flow.
Improved Maintainability: As the program grows, decoupling its numerous components (Actions, Dispatcher, Stores, and Views) makes the code modular and easy to maintain.
Predictability:React apps are resilient and maintainable because of predictability. The React ecosystem’s key principles and tools enable this. A single, consistent direction of data from parent to child components in React’s one-way data flow makes child component behaviour simple and predictable.
Clarity of Concerns: The Flux in React.js architecture separates data manipulation and UI presentation by assigning components specific tasks. React components are self-contained and focused, allowing developers to split code into logical, reusable sections. Instead of having global CSS, image, and JavaScript directories, this modular approach groups related JavaScript, HTML-like markup (JSX), and styling into a component. This encourages component independence.
Conclusion
To sum up, Flux in React.js enforces a rigorous unidirectional data flow, offering a stable and predictable architecture for managing state in React apps. As applications get more complicated, Flux in React.js makes it easier to reason about, debug, and manage them by clearly dividing responsibilities among Actions, Dispatcher, Stores, and Views. Its single data flow improves predictability and minimises side effects, while its modular design encourages clarity of concerns and guarantees that UI rendering and business logic stay separate.
Flux provides a more scalable and sustainable solution than standard MVC patterns, and even if Redux has since gained popularity as a development of its ideas, Flux in React.js is still a fundamental method for comprehending and putting into practice dependable state management in React.