redux README

The project's README from github.com/reduxjs/redux. As one markdown file: README.md.

Redux LogoRedux

Redux is a JS library for predictable and maintainable global state management.

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:

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.

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

redux (reduxjs/redux)

Searching Database…

61.5K ·GitHub
这是什么东西?Redux 是一个 JavaScript 库,用来做可预测、易维护的全局状态管理——它能让应用的行为保持一致,也很方便测试。
必须搭配 React 使用吗?不用——它既可以和 React 一起用,也可以搭配任何其他视图库,用它构建的应用能在客户端、服务端和原生环境中运行。
用它新建应用最简单的方法是什么?使用官方的 Redux Toolkit + TS Vite 模板,或者用 with-redux 模板创建一个 Next.js 项目——两者都预先配置好了 Redux Toolkit 和 React-Redux,还附带一个小的示例应用。
它是不是一个很重的依赖?核心非常小——连依赖一起只有 2kB——而且核心包本身没有任何运行时依赖;丰富的插件生态都是可选的。
是不是每个应用都应该用 Redux?不是——不要因为别人说该用就去用;它适合的场景是:有适量的数据会随时间变化、需要一个唯一的数据来源,或者顶层组件的状态已经不够用了。
状态到底是怎么变化的?整个应用的状态都放在一个单一的 store 里。改变它的唯一方式是派发一个 action——一个描述“发生了什么”的对象——然后由纯 reducer 函数根据旧状态和这个 action 算出新状态。
那 Redux Toolkit 是干什么用的?它封装了 Redux 核心,加上了官方团队认为 Redux 应用必不可少的东西:内置的最佳实践、大多数任务更简单的配置方式,以及对常见错误的防范。
我应该从哪个教程开始?Redux Essentials——一个自顶向下的教程,讲解最新的推荐 API 和最佳实践;README 也建议从这里入手。Redux Fundamentals 则是自底向上的选择,从基本原理一步步搭建。
能在 React Native 应用里用吗?可以。目前还没有官方的 React Native 模板,但 README 推荐了社区模板,标准的 React Native 和 Expo 都有。
Creator
reduxjs
Size
34.9 MB
Stars
61.5K
Age
11y
Commits · 90 Days