What is flux?
Flux is an application architecture for building client-side web applications. It complements React's composable view components by utilizing a unidirectional data flow. This makes it easier to reason about your application and helps you avoid complex state management issues.
What are flux's main functionalities?
Dispatcher
The Dispatcher is a central hub that manages all data flow in a Flux application. It receives actions and dispatches them to the appropriate stores.
const { Dispatcher } = require('flux');
const dispatcher = new Dispatcher();
dispatcher.register((payload) => {
console.log('Received payload:', payload);
});
dispatcher.dispatch({ type: 'ACTION_TYPE', data: 'sample data' });
Store
Stores contain the application state and logic. They register with the Dispatcher to receive actions and update their state accordingly. They also emit change events to notify views of state changes.
const { Dispatcher } = require('flux');
const EventEmitter = require('events').EventEmitter;
const dispatcher = new Dispatcher();
class Store extends EventEmitter {
constructor() {
super();
this.data = null;
dispatcher.register(this.handleAction.bind(this));
}
handleAction(action) {
if (action.type === 'ACTION_TYPE') {
this.data = action.data;
this.emit('change');
}
}
getData() {
return this.data;
}
}
const store = new Store();
store.on('change', () => {
console.log('Store changed:', store.getData());
});
dispatcher.dispatch({ type: 'ACTION_TYPE', data: 'sample data' });
Action
Actions are simple objects that contain new data and a type property. They are dispatched to the Dispatcher, which then forwards them to the appropriate stores.
const { Dispatcher } = require('flux');
const dispatcher = new Dispatcher();
const action = {
type: 'ACTION_TYPE',
data: 'sample data'
};
dispatcher.dispatch(action);
Other packages similar to flux
redux
Redux is a predictable state container for JavaScript apps. It is more opinionated than Flux and provides a single store for the entire application state, along with middleware support for handling side effects. Redux also has a larger ecosystem and more community support compared to Flux.
mobx
MobX is a simple, scalable state management library that makes state management simple and scalable by transparently applying functional reactive programming (TFRP). Unlike Flux, MobX uses observables to track state changes and automatically update the UI, making it more intuitive and less boilerplate-heavy.
recoil
Recoil is a state management library for React that provides a way to share state across components with minimal boilerplate. It is designed to work seamlessly with React's concurrent mode and provides a more modern approach to state management compared to Flux.
⚠️ The Flux project is in maintenance mode and there are many more sophisticated alternatives available (e.g. Redux, MobX) and we would recommend using them instead.
Flux
An application architecture for React utilizing a unidirectional data flow.
Getting Started
Start by looking through the guides and examples on Github. For more resources and API docs check out facebook.github.io/flux.
How Flux works
For more information on how Flux works check out the Flux Concepts guide, or the In Depth Overview.
Requirements
Flux is more of a pattern than a framework, and does not have any hard dependencies. However, we often use EventEmitter as a basis for Stores
and React for our Views
. The one piece of Flux not readily available elsewhere is the Dispatcher
. This module, along with some other utilities, is available here to complete your Flux toolbox.
Installing Flux
Flux is available as a npm module, so you can add it to your package.json file or run npm install flux
. The dispatcher will be available as Flux.Dispatcher
and can be required like this:
const Dispatcher = require('flux').Dispatcher;
Take a look at the dispatcher API and some examples.
Flux Utils
We have also provided some basic utility classes to help get you started with Flux. These base classes are a solid foundation for a simple Flux application, but they are not a feature-complete framework that will handle all use cases. There are many other great Flux frameworks out there if these utilities do not fulfill your needs.
import {ReduceStore} from 'flux/utils';
class CounterStore extends ReduceStore<number> {
getInitialState(): number {
return 0;
}
reduce(state: number, action: Object): number {
switch (action.type) {
case 'increment':
return state + 1;
case 'square':
return state * state;
default:
return state;
}
}
}
Check out the examples and documentation for more information.
Building Flux from a Cloned Repo
Clone the repo and navigate into the resulting flux
directory. Then run npm install
.
This will run Gulp-based build tasks automatically and produce the file Flux.js, which you can then require as a module.
You could then require the Dispatcher like so:
const Dispatcher = require('path/to/this/directory/Flux').Dispatcher;
The build process also produces de-sugared versions of the Dispatcher
and invariant
modules in a lib
directory, and you can require those modules directly, copying them into whatever directory is most convenient for you. The flux-todomvc and flux-chat example applications both do this.
See the CONTRIBUTING file for how to help out.
License
Flux is BSD-licensed. We also provide an additional patent grant.