Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

flux

facebook3.8mBSD-3-Clause4.0.4TypeScript support: definitely-typed

An application architecture based on a unidirectional data flow

flux, react, facebook, dispatcher

readme

⚠️ The Flux project has been archived and no further changes will be made. We recommend using more sophisticated alternatives like Redux, MobX, Recoil, Zustand, or Jotai.

logo

Flux

An application architecture for React utilizing a unidirectional data flow.
Licence Badge Current npm package version.


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.

License

Flux is BSD-licensed. We also provide an additional patent grant.

changelog

Changelog

4.0.3

4.0.2

  • Added support for UNSAFE_componentWillReceiveProps lifecycle method

4.0.1

  • Upgrade fbemitter dependency to 3.x

4.0.0

  • Upgrade fbjs dependency to ^3.x
  • Upgrade for Babel 7 compatibility (#495) (thanks to @koba04)
  • Added React 17 as a peer dependency

3.1.3

  • Added support for React 16

3.1.2

  • No meaningful changes.

3.1.1

  • No meaningful changes.

3.1.0

  • Dispatcher: Methods register and unregister can once again be called in the middle of a dispatch.

3.0.0

  • FluxMapStore: Removed. It added very little value over FluxReduceStore.
  • FluxContainer: Subscriptions are setup in constructor rather than componentDidMount
  • FluxContainer: Can create containers using stateless functional components
  • FluxContainer: Uses functional version of setState
  • FluxMixin: Subscriptions are setup in componentWillMount rather than componentDidMount
  • Dispatcher: Methods register and unregister can not be called in the middle of a dispatch
  • React added as peer dependency to flux/utils
  • Package dist/FluxUtils.js alongside dist/Flux.js

Note: This is marked as a breaking change due to the large number of small changes in FluxContainer. Depending on how coupled code is to the timing of componentWillMount, componentDidMount, or setting state synchronously it is possible that there may be some breakages. Generally it should not be an issue.

2.1.1

  • Publish dist/ on npm

2.1.0

  • Add flux-utils which include four main base classes: Store, ReduceStore, MapStore, Container
  • Add flux-utils example and documentation
  • Upgrade build script
  • Publish a minified version of Flux in dist/
  • Add flow types to Dispatcher