redux vs vuex vs react-query vs effector vs mobx vs recoil vs xstate vs zustand
状态管理库
reduxvuexreact-queryeffectormobxrecoilxstatezustand类似的npm包:

状态管理库

状态管理库是用于管理和维护应用程序状态的工具,它们帮助开发者在复杂的应用中保持状态的一致性和可预测性。通过这些库,开发者可以更轻松地管理数据流、处理异步请求、以及在组件之间共享状态。选择合适的状态管理库可以显著提高应用的可维护性和可扩展性。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
redux23,556,81761,459290 kB412 年前MIT
vuex1,386,72328,394271 kB142-MIT
react-query1,325,25548,8572.26 MB1493 年前MIT
effector59,0544,8211.59 MB1536 个月前MIT
mobx028,1834.35 MB806 个月前MIT
recoil019,5202.21 MB3223 年前MIT
xstate029,3382.25 MB1521 个月前MIT
zustand057,45995 kB43 天前MIT

功能对比: redux vs vuex vs react-query vs effector vs mobx vs recoil vs xstate vs zustand

设计原则

  • redux:

    Redux采用单向数据流和单一状态树的设计原则,使得状态管理更加可预测和易于调试。

  • vuex:

    Vuex遵循集中式状态管理的原则,适合大型Vue应用,确保状态的可追踪性和一致性。

  • react-query:

    React Query专注于服务器状态管理,提供了数据获取、缓存和同步的功能,简化了异步数据处理的复杂性。

  • effector:

    Effector采用函数式编程的设计理念,强调状态的不可变性和事件驱动的更新方式,使得状态管理更加清晰和可预测。

  • mobx:

    MobX基于反应式编程,允许开发者以声明的方式定义状态,自动跟踪依赖关系,简化了状态更新的过程。

  • recoil:

    Recoil通过原子和选择器的概念,提供了灵活的状态管理方式,允许开发者以更细粒度的方式管理状态。

  • xstate:

    XState采用状态机的设计理念,允许开发者以可视化的方式定义状态和状态转移,适合复杂的状态管理需求。

  • zustand:

    Zustand提供了简单的API和灵活的状态管理方式,强调轻量级和高性能,适合快速开发和小型项目。

学习曲线

  • redux:

    Redux的学习曲线较陡,特别是对于初学者。理解其中间件、动作和状态管理的概念需要一定的时间和实践。

  • vuex:

    Vuex的学习曲线适中,特别是对于熟悉Vue的开发者。其集中式状态管理的概念易于理解,但在大型应用中可能需要更多的时间来掌握。

  • react-query:

    React Query的学习曲线较低,提供了直观的API,开发者可以快速上手并实现数据获取和缓存。

  • effector:

    Effector的学习曲线相对较陡,特别是对于不熟悉函数式编程的开发者。理解其事件和状态的概念需要一定的时间。

  • mobx:

    MobX的学习曲线较为平缓,特别是对于熟悉React的开发者。其反应式编程的概念易于理解,适合快速上手。

  • recoil:

    Recoil的学习曲线适中,特别是对于已经熟悉React的开发者。其原子和选择器的概念需要一些时间来掌握。

  • xstate:

    XState的学习曲线较陡,特别是对于不熟悉状态机的开发者。理解其状态图和状态转移的概念需要一定的时间。

  • zustand:

    Zustand的学习曲线较低,提供了简单的API,开发者可以快速上手并实现状态管理。

可扩展性

  • redux:

    Redux的可扩展性体现在其中间件和插件机制上,允许开发者根据需求扩展状态管理的功能。

  • vuex:

    Vuex的可扩展性体现在其模块化设计上,允许开发者根据需求拆分和管理状态。

  • react-query:

    React Query的可扩展性体现在其插件机制上,开发者可以根据需求扩展数据获取和缓存的功能。

  • effector:

    Effector具有很高的可扩展性,支持自定义事件和状态逻辑,适合复杂应用的需求。

  • mobx:

    MobX的可扩展性较强,允许开发者通过装饰器和中间件扩展其功能,适合多种应用场景。

  • recoil:

    Recoil的可扩展性体现在原子和选择器的组合使用上,允许开发者根据需求灵活管理状态。

  • xstate:

    XState的可扩展性体现在其状态机的定义上,允许开发者根据需求灵活管理复杂状态逻辑。

  • zustand:

    Zustand的可扩展性较强,允许开发者根据需求自定义状态管理的逻辑,适合多种应用场景。

性能

  • redux:

    Redux的性能依赖于状态树的设计和中间件的使用,适合需要高可预测性的应用。

  • vuex:

    Vuex的性能在于其响应式系统的优化,适合需要高性能的Vue应用。

  • react-query:

    React Query通过数据缓存和智能更新机制,显著提高了数据获取的性能,适合需要频繁请求的应用。

  • effector:

    Effector在性能方面表现优异,采用了高效的事件处理机制,适合需要高性能的应用。

  • mobx:

    MobX通过自动跟踪依赖关系,优化了状态更新的性能,适合快速响应的应用。

  • recoil:

    Recoil在性能方面表现良好,通过原子和选择器的机制,优化了状态更新的性能,适合中大型应用。

  • xstate:

    XState在处理复杂状态转移时表现良好,适合需要高性能状态管理的应用。

  • zustand:

    Zustand的性能表现优异,采用了轻量级的状态管理机制,适合小型项目和快速开发。

如何选择: redux vs vuex vs react-query vs effector vs mobx vs recoil vs xstate vs zustand

  • redux:

    选择Redux如果你需要一个可预测的状态容器,适合大型应用。Redux的单一状态树和严格的状态更新机制使得调试和测试变得简单。

  • vuex:

    选择Vuex如果你在使用Vue.js并需要集中式状态管理。Vuex与Vue的响应式系统紧密集成,适合中大型Vue应用。

  • react-query:

    选择React Query如果你主要处理服务器状态和异步数据获取。它提供了强大的数据缓存、同步和更新机制,简化了数据获取的复杂性。

  • effector:

    选择Effector如果你需要一个高性能、灵活且支持函数式编程的状态管理库。它提供了强大的事件和状态机制,适合需要复杂状态逻辑的应用。

  • mobx:

    选择MobX如果你喜欢简单的、自动化的状态管理方式。MobX使用可观察的状态和反应式编程,使得状态变化自动更新UI,适合快速开发和小型项目。

  • recoil:

    选择Recoil如果你在使用React并需要一个简单易用的状态管理解决方案。Recoil允许你在组件之间共享状态,并提供了原子和选择器的概念,增强了状态的可组合性。

  • xstate:

    选择XState如果你需要管理复杂的状态机和状态图。它提供了可视化的状态管理和强大的状态转换机制,适合需要复杂状态逻辑的应用。

  • zustand:

    选择Zustand如果你需要一个轻量级且简单的状态管理库。Zustand提供了简洁的API和灵活的状态管理方式,适合小型项目和快速开发。

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