redux vs mobx vs vuex
状态管理库
reduxmobxvuex类似的npm包:

状态管理库

状态管理库是用于管理应用程序状态的工具,特别是在构建复杂的用户界面时。它们提供了一种集中式的方式来存储和管理状态,使得组件之间的状态共享和更新变得更加高效和可预测。选择合适的状态管理库可以帮助开发者简化数据流,增强应用的可维护性和可扩展性。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
redux25,266,19761,440290 kB432 年前MIT
mobx3,127,64128,1824.35 MB865 个月前MIT
vuex1,741,56528,395271 kB142-MIT

功能对比: redux vs mobx vs vuex

设计原则

  • redux:

    Redux 采用单向数据流和不可变数据原则,确保状态的可预测性和可调试性。所有状态的变化都通过派发动作来实现,使得状态变化的过程清晰可追踪。

  • mobx:

    MobX 采用响应式编程原则,允许状态的自动更新和依赖追踪。它通过可观察的状态和计算属性来简化数据流,使得状态管理更加直观和灵活。

  • vuex:

    Vuex 设计为与 Vue.js 深度集成,采用集中式存储和模块化结构,确保状态管理的一致性和可维护性。它利用 Vue 的响应式特性,使得状态变化自动反映在视图中。

学习曲线

  • redux:

    Redux 的学习曲线较陡峭,尤其是对于初学者。理解中间件、不可变数据和 Redux 的工作流程需要一定的时间和实践。

  • mobx:

    MobX 的学习曲线相对较平缓,特别是对于已经熟悉 JavaScript 的开发者。它的 API 简单易懂,适合快速上手和小型项目。

  • vuex:

    Vuex 的学习曲线与 Vue.js 的学习曲线相似,适合已经熟悉 Vue 的开发者。它的 API 设计与 Vue 的组件系统一致,使得学习过程更加顺畅。

性能

  • redux:

    Redux 的性能主要依赖于如何管理状态和使用中间件。通过合理的状态切片和使用 memoization,可以有效提升性能。

  • mobx:

    MobX 通过细粒度的观察和自动依赖追踪来优化性能。只有当相关的状态发生变化时,组件才会重新渲染,从而提高性能。

  • vuex:

    Vuex 的性能受益于 Vue 的响应式系统,状态变化会自动更新视图。通过模块化和懒加载,可以进一步优化大型应用的性能。

可扩展性

  • redux:

    Redux 的可扩展性体现在其中间件机制上,允许开发者添加自定义逻辑和功能,适合复杂应用的需求。

  • mobx:

    MobX 允许开发者通过简单的 API 扩展功能,支持中间件和插件,适合快速迭代和扩展。

  • vuex:

    Vuex 的模块化设计使得状态管理更加灵活,支持按需加载和插件,适合大型 Vue 应用的扩展。

调试工具

  • redux:

    Redux 拥有强大的调试工具,如 Redux DevTools,允许开发者追踪每一个状态变化和动作,极大地提升了调试效率。

  • mobx:

    MobX 提供了开发者工具,允许实时查看可观察状态和计算属性,方便调试和性能分析。

  • vuex:

    Vuex 也提供了 Vue DevTools 的支持,可以实时监控状态变化和时间旅行调试,方便开发者进行调试和优化。

如何选择: redux vs mobx vs vuex

  • redux:

    选择 Redux 如果你需要一个可预测的状态容器,特别是在大型应用中。Redux 提供了一个单一的状态树和不可变数据结构,适合需要复杂状态管理和调试的场景。

  • mobx:

    选择 MobX 如果你需要一个简单且灵活的状态管理解决方案,特别是当你的应用程序需要响应式编程时。MobX 通过可观察的状态和自动依赖追踪来简化状态管理,适合中小型应用。

  • vuex:

    选择 Vuex 如果你正在使用 Vue.js 框架,并且需要一个专门为 Vue 设计的状态管理解决方案。Vuex 提供了与 Vue 的深度集成,支持模块化和插件,适合 Vue 应用的状态管理。

redux的README

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