redux vs mobx vs flux
State Management Libraries Comparison
3 Years
reduxmobxfluxSimilar Packages:
What's State Management Libraries?

State management libraries in JavaScript help manage and centralize the application state, making it easier to share data between components, handle complex state logic, and maintain a predictable state across the application. These libraries provide patterns and tools for managing state changes, ensuring that the UI stays in sync with the underlying data. They are particularly useful in large applications where state management can become complex and challenging to handle with local component state alone. redux is a predictable state container for JavaScript apps, mobx is a simple, scalable state management solution that uses observables, and flux is an application architecture for building client-side web applications with unidirectional data flow.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
redux14,478,108
61,331290 kB452 years agoMIT
mobx2,005,752
28,0324.33 MB786 months agoMIT
flux849,188
17,510260 kB62 years agoBSD-3-Clause
Feature Comparison: redux vs mobx vs flux

State Management Approach

  • redux:

    redux also follows a unidirectional data flow model but enforces a stricter structure with a single store, actions, and reducers. This makes state changes predictable and easier to track, especially in large applications.

  • mobx:

    mobx takes a more reactive approach to state management, using observables to automatically track changes and update the UI. This allows for more dynamic and efficient state updates without the need for explicit actions or reducers.

  • flux:

    flux uses a unidirectional data flow model, where data flows in one direction, making it easier to understand and debug state changes. It consists of actions, stores, and a dispatcher, allowing for a clear separation of concerns.

Boilerplate Code

  • redux:

    redux is known for its boilerplate, especially when defining actions, reducers, and the store. However, tools like Redux Toolkit have been introduced to reduce boilerplate and simplify the setup process.

  • mobx:

    mobx has minimal boilerplate compared to redux and flux, as it leverages decorators and observables to manage state. This makes it quicker to implement and easier to maintain.

  • flux:

    flux requires some boilerplate code to set up actions, stores, and the dispatcher, but it is flexible and allows for customization based on the application's needs.

Reactivity

  • redux:

    redux also lacks built-in reactivity, but libraries like react-redux provide hooks and higher-order components to connect components to the store and re-render them on state changes.

  • mobx:

    mobx excels in reactivity, automatically updating the UI when observable state changes. This leads to more efficient and less error-prone updates, as components only re-render when the specific data they depend on changes.

  • flux:

    flux does not provide built-in reactivity; updates to the UI must be handled manually by subscribing to store changes.

Ecosystem and Community

  • redux:

    redux has a large and active ecosystem, with numerous middleware, dev tools, and libraries built around it. This makes it a well-supported choice for state management in JavaScript applications.

  • mobx:

    mobx has a growing community and ecosystem, particularly among developers who prefer reactive programming. It may not be as large as redux, but it is well-supported and documented.

  • flux:

    flux has a smaller ecosystem compared to redux, as it is more of a design pattern than a fully-fledged library. However, it has influenced many other state management solutions.

Ease of Use: Code Examples

  • redux:

    redux example

    import { createStore } from 'redux';
    
    // Action
    const addTodo = (text) => ({ type: 'ADD_TODO', text });
    
    // Reducer
    const todoReducer = (state = [], action) => {
      switch (action.type) {
        case 'ADD_TODO':
          return [...state, { text: action.text }];
        default:
          return state;
      }
    };
    
    // Store
    const store = createStore(todoReducer);
    
    // Component
    const TodoList = () => {
      const todos = store.getState();
      return <ul>{todos.map((todo) => <li>{todo.text}</li>)}</ul>;
    };
    
  • mobx:

    mobx example

    import { observable, action } from 'mobx';
    import { observer } from 'mobx-react';
    
    // Store
    class TodoStore {
      @observable todos = [];
    
      @action addTodo(text) {
        this.todos.push({ text });
      }
    }
    
    const todoStore = new TodoStore();
    
    // Component
    const TodoList = observer(() => {
      return <ul>{todoStore.todos.map((todo) => <li>{todo.text}</li>)}</ul>;
    });
    
  • flux:

    flux example

    // Action
    const addTodo = (text) => ({ type: 'ADD_TODO', text });
    
    // Store
    class TodoStore {
      constructor() {
        this.todos = [];
        this.listeners = [];
      }
    
      addTodo(todo) {
        this.todos.push(todo);
        this.notify();
      }
    
      notify() {
        this.listeners.forEach((listener) => listener());
      }
    
      subscribe(listener) {
        this.listeners.push(listener);
      }
    }
    
    const todoStore = new TodoStore();
    
    // Component
    const TodoList = () => {
      const [todos, setTodos] = useState(todoStore.todos);
    
      useEffect(() => {
        const listener = () => setTodos(todoStore.todos);
        todoStore.subscribe(listener);
      }, []);
    
      return <ul>{todos.map((todo) => <li>{todo.text}</li>)}</ul>;
    };
    
How to Choose: redux vs mobx vs flux
  • redux:

    Choose redux if you need a predictable state container with a strict structure, middleware support, and a large ecosystem of tools and libraries. It is ideal for applications that require time-travel debugging, state persistence, and a clear separation of concerns.

  • mobx:

    Choose mobx if you prefer a more reactive and less boilerplate-heavy approach to state management. It is suitable for applications that benefit from fine-grained reactivity and where performance is a concern due to its efficient use of observables.

  • flux:

    Choose flux if you want to implement a unidirectional data flow architecture in your application. It is a pattern rather than a library, providing flexibility in how you manage state and actions.

README for redux

Redux Logo

Redux is a predictable state container for JavaScript apps.

It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time traveling debugger.

You can use Redux together with React, or with any other view library. The Redux core is tiny (2kB, including dependencies), and has a rich ecosystem of addons.

Redux Toolkit is our official recommended approach for writing Redux logic. It wraps around the Redux core, and contains packages and functions that we think are essential for building a Redux app. Redux Toolkit builds in our suggested best practices, simplifies most Redux tasks, prevents common mistakes, and makes it easier to write Redux applications.

GitHub Workflow Status npm version npm downloads redux channel on discord

Installation

Create a React Redux App

The recommended way to start new apps with React and Redux Toolkit is by using our official Redux Toolkit + TS template for Vite, or by creating a new Next.js project using Next's with-redux template.

Both of these already have Redux Toolkit and React-Redux configured appropriately for that build tool, and come with a small example app that demonstrates how to use several of Redux Toolkit's features.

# Vite with our Redux+TS template
# (using the `degit` tool to clone and extract the template)
npx degit reduxjs/redux-templates/packages/vite-template-redux my-app

# Next.js using the `with-redux` template
npx create-next-app --example with-redux my-app

We do not currently have official React Native templates, but recommend these templates for standard React Native and for Expo:

  • https://github.com/rahsheen/react-native-template-redux-typescript
  • https://github.com/rahsheen/expo-template-redux-typescript
npm install @reduxjs/toolkit react-redux

For the Redux core library by itself:

npm install redux

For more details, see the Installation docs page.

Documentation

The Redux core docs are located at https://redux.js.org, and include the full Redux tutorials, as well usage guides on general Redux patterns:

The Redux Toolkit docs are available at https://redux-toolkit.js.org, including API references and usage guides for all of the APIs included in Redux Toolkit.

Learn Redux

Redux Essentials Tutorial

The Redux Essentials tutorial is a "top-down" tutorial that teaches "how to use Redux the right way", using our latest recommended APIs and best practices. We recommend starting there.

Redux Fundamentals Tutorial

The Redux Fundamentals tutorial is a "bottom-up" tutorial that teaches "how Redux works" from first principles and without any abstractions, and why standard Redux usage patterns exist.

Help and Discussion

The #redux channel of the Reactiflux Discord community is our official resource for all questions related to learning and using Redux. Reactiflux is a great place to hang out, ask questions, and learn - please come and join us there!

Before Proceeding Further

Redux is a valuable tool for organizing your state, but you should also consider whether it's appropriate for your situation. Please don't use Redux just because someone said you should - instead, please take some time to understand the potential benefits and tradeoffs of using it.

Here are some suggestions on when it makes sense to use Redux:

  • You have reasonable amounts of data changing over time
  • You need a single source of truth for your state
  • You find that keeping all your state in a top-level component is no longer sufficient

Yes, these guidelines are subjective and vague, but this is for a good reason. The point at which you should integrate Redux into your application is different for every user and different for every application.

For more thoughts on how Redux is meant to be used, please see:

Basic Example

The whole global state of your app is stored in an object tree inside a single store. The only way to change the state tree is to create an action, an object describing what happened, and dispatch it to the store. To specify how state gets updated in response to an action, you write pure reducer functions that calculate a new state based on the old state and the action.

Redux Toolkit simplifies the process of writing Redux logic and setting up the store. With Redux Toolkit, the basic app logic looks like:

import { createSlice, configureStore } from '@reduxjs/toolkit'

const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    value: 0
  },
  reducers: {
    incremented: state => {
      // Redux Toolkit allows us to write "mutating" logic in reducers. It
      // doesn't actually mutate the state because it uses the Immer library,
      // which detects changes to a "draft state" and produces a brand new
      // immutable state based off those changes
      state.value += 1
    },
    decremented: state => {
      state.value -= 1
    }
  }
})

export const { incremented, decremented } = counterSlice.actions

const store = configureStore({
  reducer: counterSlice.reducer
})

// Can still subscribe to the store
store.subscribe(() => console.log(store.getState()))

// Still pass action objects to `dispatch`, but they're created for us
store.dispatch(incremented())
// {value: 1}
store.dispatch(incremented())
// {value: 2}
store.dispatch(decremented())
// {value: 1}

Redux Toolkit allows us to write shorter logic that's easier to read, while still following the original core Redux behavior and data flow.

Logo

You can find the official logo on GitHub.

Change Log

This project adheres to Semantic Versioning. Every release, along with the migration instructions, is documented on the GitHub Releases page.

License

MIT