redux vs jotai vs mobx vs react-query vs recoil vs valtio vs xstate vs zustand
状态管理库
reduxjotaimobxreact-queryrecoilvaltioxstatezustand类似的npm包:

状态管理库

状态管理库是用于管理应用程序状态的工具,特别是在复杂的用户界面中。它们帮助开发者在不同组件之间共享和管理状态,确保数据一致性和可预测性。这些库各有特点,适用于不同的场景和需求,选择合适的状态管理库可以显著提高开发效率和应用性能。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
redux21,613,26561,440290 kB432 年前MIT
jotai2,825,82121,019517 kB36 天前MIT
mobx2,810,62828,1804.35 MB855 个月前MIT
react-query1,460,43748,5962.26 MB1743 年前MIT
recoil454,83919,5272.21 MB3223 年前MIT
valtio010,125101 kB22 个月前MIT
xstate029,2652.25 MB16612 天前MIT
zustand057,11695 kB424 天前MIT

功能对比: redux vs jotai vs mobx vs react-query vs recoil vs valtio vs xstate vs zustand

状态管理模式

  • redux:

    Redux 使用集中式状态管理,通过 actions 和 reducers 管理状态变化,适合大型应用的可预测性和可维护性。

  • jotai:

    Jotai 使用原子状态管理,每个状态都是独立的原子,允许局部更新,减少不必要的重渲染。

  • mobx:

    MobX 使用可观察的状态和自动依赖追踪,允许状态变化自动更新相关组件,适合响应式编程。

  • react-query:

    React Query 专注于服务器状态管理,提供数据获取、缓存和同步功能,简化了与后端的交互。

  • recoil:

    Recoil 允许创建原子状态和派生状态,支持复杂的状态逻辑和依赖关系,适合大型应用。

  • valtio:

    Valtio 使用代理模式,提供简单的状态管理,支持直接修改状态,适合快速开发。

  • xstate:

    XState 使用状态机模型,明确状态转换和行为,适合需要复杂状态管理的应用。

  • zustand:

    Zustand 提供简单的 API,支持直接修改状态,适合小型项目的快速开发。

学习曲线

  • redux:

    Redux 的学习曲线较陡,特别是对于初学者,需要理解 actions、reducers 和中间件的概念。

  • jotai:

    Jotai 的学习曲线较低,易于上手,特别适合熟悉 React Hooks 的开发者。

  • mobx:

    MobX 的学习曲线适中,提供了简单的 API,但需要理解可观察的概念。

  • react-query:

    React Query 的学习曲线较低,易于集成和使用,特别适合处理服务器数据。

  • recoil:

    Recoil 的学习曲线适中,特别适合已经熟悉 React 的开发者。

  • valtio:

    Valtio 的学习曲线较低,API 直观,适合快速上手。

  • xstate:

    XState 的学习曲线较陡,需要理解状态机的概念和状态转换。

  • zustand:

    Zustand 的学习曲线较低,API 简单易懂,适合快速开发。

性能

  • redux:

    Redux 的性能在于其可预测性和调试工具,适合大型应用的状态管理。

  • jotai:

    Jotai 通过原子状态管理减少了不必要的重渲染,性能表现良好。

  • mobx:

    MobX 的性能依赖于自动依赖追踪,能够高效地更新相关组件,适合高频率的状态变化。

  • react-query:

    React Query 提供了高效的数据缓存和更新机制,能够显著提高数据获取性能。

  • recoil:

    Recoil 的性能表现良好,特别是在处理复杂状态时,能够有效管理依赖关系。

  • valtio:

    Valtio 的性能良好,直接修改状态的方式使得状态更新高效。

  • xstate:

    XState 的性能依赖于状态机的设计,能够高效地管理复杂状态。

  • zustand:

    Zustand 的性能表现良好,简单的 API 和直接的状态管理使得更新高效。

扩展性

  • redux:

    Redux 的中间件机制允许扩展功能,适合大型应用的需求。

  • jotai:

    Jotai 允许通过原子组合和自定义 hooks 扩展功能,适合灵活的状态管理需求。

  • mobx:

    MobX 提供了丰富的插件和扩展机制,适合复杂应用的需求。

  • react-query:

    React Query 提供了丰富的 API 和配置选项,支持自定义数据获取和缓存策略。

  • recoil:

    Recoil 的原子和派生状态机制使得扩展性良好,适合复杂的状态管理需求。

  • valtio:

    Valtio 的简单 API 使得扩展性良好,适合快速开发和迭代。

  • xstate:

    XState 的状态机模型允许灵活扩展,适合复杂应用的需求。

  • zustand:

    Zustand 的简单 API 使得扩展性良好,适合快速开发和小型项目。

如何选择: redux vs jotai vs mobx vs react-query vs recoil vs valtio vs xstate vs zustand

  • redux:

    选择 Redux 如果你的应用需要集中式的状态管理,尤其是在大型应用中。Redux 提供了强大的中间件支持和调试工具,适合需要严格状态管理的场景。

  • jotai:

    选择 Jotai 如果你需要一个简单且灵活的状态管理解决方案,特别是在使用 React Hooks 的项目中。它的原子状态管理使得状态更新更为局部化,避免了不必要的重渲染。

  • mobx:

    选择 MobX 如果你偏好使用可观察的状态管理,并且需要自动化的依赖追踪。MobX 提供了简单的 API 和强大的性能,适合需要响应式编程的应用。

  • react-query:

    选择 React Query 如果你的应用需要处理服务器状态,特别是数据获取和缓存。它提供了强大的数据获取、缓存和更新功能,简化了与后端的交互。

  • recoil:

    选择 Recoil 如果你需要在 React 应用中实现更细粒度的状态管理。Recoil 允许你创建原子状态和派生状态,适合复杂的状态逻辑。

  • valtio:

    选择 Valtio 如果你想要一个轻量级且简单的状态管理库,支持代理模式。它的 API 直观,适合快速开发和小型项目。

  • xstate:

    选择 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