redux vs mobx vs vuex vs dva
State Management Libraries
reduxmobxvuexdvaSimilar Packages:
State Management Libraries

State management libraries are essential tools in modern web development, particularly for applications built with frameworks like React and Vue.js. These libraries help manage the state (data) of an application in a predictable and organized manner, making it easier to share data between components, handle user interactions, and maintain a consistent UI. They provide a centralized way to manage state, allowing for better control, debugging, and testing of an application's data flow. Popular state management libraries include Redux, MobX, Vuex, and Dva, each with its own approach and features.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
redux16,354,36261,436290 kB482 years agoMIT
mobx2,215,39928,1524.35 MB803 months agoMIT
vuex1,299,38528,407271 kB143-MIT
dva20,68816,214902 kB28-MIT
Feature Comparison: redux vs mobx vs vuex vs dva

Architecture

  • redux:

    Redux follows a unidirectional data flow architecture with a single store, actions, and reducers. State changes are made through pure functions (reducers) that take the current state and an action as arguments, ensuring predictability and easy debugging.

  • mobx:

    MobX uses a reactive programming model with observables, actions, and computed values. State is stored in observables, and the UI automatically updates when observables change, reducing the need for manual state management and making it more intuitive for developers.

  • vuex:

    Vuex is designed specifically for Vue.js applications and follows a similar architecture to Redux but with Vue's reactivity system. It has a centralized store, mutations (synchronous state changes), actions (asynchronous), and getters (computed properties), making it easy to manage state in Vue components.

  • dva:

    Dva combines concepts from Redux and Flux, providing a more opinionated and simplified approach to state management. It integrates Redux for state management, Redux-Saga for handling side effects, and supports routing out of the box, making it a comprehensive solution for React applications.

Learning Curve

  • redux:

    Redux has a steeper learning curve due to its strict architecture, boilerplate code, and the need to understand concepts like middleware, reducers, and actions. However, its predictability and ecosystem make it worth the investment for large applications.

  • mobx:

    MobX has a gentler learning curve, especially for developers familiar with reactive programming. Its use of decorators and observables makes it easy to understand and use, with less boilerplate compared to Redux.

  • vuex:

    Vuex is relatively easy to learn for developers already familiar with Vue.js. Its integration with Vue's reactivity system and clear separation of state, mutations, actions, and getters make it intuitive for Vue developers.

  • dva:

    Dva has a moderate learning curve, especially for those new to Redux and Redux-Saga. However, its conventions and built-in routing make it easier to grasp for developers familiar with React and Ant Design.

Boilerplate Code

  • redux:

    Redux is known for its boilerplate code, as it requires setting up actions, reducers, and the store manually. However, this boilerplate enforces a clear structure and makes the data flow predictable, which is beneficial for large applications.

  • mobx:

    MobX minimizes boilerplate code by allowing developers to define state and actions more freely. The use of decorators and observables reduces the need for explicit action creators and reducers, making the code more concise and easier to read.

  • vuex:

    Vuex requires some boilerplate to set up the store, mutations, actions, and getters. However, this structure helps maintain a clear and organized state management system, especially in larger Vue applications.

  • dva:

    Dva reduces boilerplate by combining state management, side effects, and routing into a single framework. It provides a more streamlined approach to managing state and side effects, which can lead to cleaner and more maintainable code.

Integration with Frameworks

  • redux:

    Redux is framework-agnostic and can be used with any JavaScript framework or library, including React, Angular, and Vue. Its flexibility and wide adoption have led to a large ecosystem of tools and middleware.

  • mobx:

    MobX is also framework-agnostic but is particularly popular in React applications due to its reactive nature. It can be easily integrated with any framework that supports JavaScript, making it versatile and adaptable.

  • vuex:

    Vuex is specifically designed for Vue.js applications and integrates seamlessly with Vue's reactivity system. It is the go-to state management solution for Vue developers and is optimized for use with Vue components.

  • dva:

    Dva is designed for React applications and integrates well with Ant Design. It combines state management, side effects, and routing, making it a comprehensive solution for building React applications with a focus on simplicity and convention.

Ease of Use: Code Examples

  • redux:

    Redux Example: Simple Counter

    import { createStore } from 'redux';
    
    // Action
    const increment = () => ({ type: 'INCREMENT' });
    
    // Reducer
    const counterReducer = (state = 0, action) => {
      switch (action.type) {
        case 'INCREMENT':
          return state + 1;
        default:
          return state;
      }
    };
    
    // Store
    const store = createStore(counterReducer);
    
    // Subscribe
    store.subscribe(() => console.log(store.getState()));
    
    // Dispatch
    store.dispatch(increment()); // Output: 1
    
  • mobx:

    MobX Example: Simple Counter

    import { observable, action } from 'mobx';
    import { observer } from 'mobx-react';
    import React from 'react';
    
    // Store
    class CounterStore {
      @observable count = 0;
    
      @action increment() {
        this.count++;
      }
    }
    
    const counterStore = new CounterStore();
    
    // Component
    const Counter = observer(() => (
      <div>
        <p>Count: {counterStore.count}</p>
        <button onClick={() => counterStore.increment()}>Increment</button>
      </div>
    ));
    
    // Usage
    const App = () => <Counter />;
    
  • vuex:

    Vuex Example: Simple Counter

    import Vue from 'vue';
    import Vuex from 'vuex';
    
    Vue.use(Vuex);
    
    // Store
    const store = new Vuex.Store({
      state: { count: 0 },
      mutations: {
        increment(state) {
          state.count++;
        },
      },
    });
    
    // Component
    new Vue({
      el: '#app',
      store,
      computed: {
        count() {
          return this.$store.state.count;
        },
      },
      methods: {
        increment() {
          this.$store.commit('increment');
        },
      },
      template: `<div>
        <p>Count: {{ count }}</p>
        <button @click="increment">Increment</button>
      </div>`,
    });
    
  • dva:

    Dva Example: Simple Counter

    import dva from 'dva';
    
    // 1. Initialize
    const app = dva();
    
    // 2. Model
    app.model({
      namespace: 'counter',
      state: 0,
      reducers: {
        increment(state) {
          return state + 1;
        },
      },
    });
    
    // 3. View
    app.view(() => {
      const count = app._store.getState().counter;
      return (
        <div>
          <p>Count: {count}</p>
          <button onClick={() => app._store.dispatch({ type: 'counter/increment' })}>Increment</button>
        </div>
      );
    });
    
    // 4. Start
    app.start('#app');
    
How to Choose: redux vs mobx vs vuex vs dva
  • redux:

    Choose Redux if you need a predictable state container with a unidirectional data flow, time-travel debugging, and a large ecosystem of middleware and tools. It is ideal for large applications with complex state management needs.

  • mobx:

    Choose MobX if you prefer a more reactive and less boilerplate-heavy approach to state management. It is great for applications where you want to automatically update the UI based on state changes without manually dispatching actions.

  • vuex:

    Choose Vuex if you are building a Vue.js application and need a state management solution that integrates seamlessly with Vue's reactivity system. It provides a centralized store, mutations, actions, and getters, making it easy to manage state in Vue components.

  • dva:

    Choose Dva if you are working on a React or Ant Design project and want a framework that combines Redux, Redux-Saga, and routing with a focus on simplicity and convention over configuration. It is particularly useful for applications that require side effects management and asynchronous 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:

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