Redux 安装

Redux 安装

Redux作为NPM上的软件包提供,可与模块捆绑器或Node应用程序一起使用:

# NPM
npm install redux

# Yarn
yarn add redux

它也可以作为预编译的UMD软件包来使用,该软件包定义了window.Redux全局变量。

UMD包可以直接用作<script>标签

安装 Redux 稳定版

npm install --save redux

以上基于使用 npm 来做包管理工具的情况下。

否则你可以直接在 unpkg 上访问这些文件,下载下来,或者把让你的包管理工具指向它。

一般情况下人们认为 Redux 就是一些 CommonJS 模块的集合。这些模块就是你在使用 WebpackBrowserify、或者 Node 环境时引入的。如果你想追求时髦并使用 Rollup,也是支持的。

你也可以不使用模块打包工具。redux 的 npm 包里 dist 目录包含了预编译好的生产环境和开发环境下的 UMD 文件。可以直接使用,而且支持大部分流行的 JavaScript 包加载器和环境。比如,你可以直接在页面上的 <script> 标签 中引入 UMD 文件,也可以让 Bower 来安装。UMD 文件可以让你使用 window.Redux 全局变量来访问 Redux。

Redux 源文件由 ES2015 编写,但是会预编译到 CommonJS 和 UMD 规范的 ES5,所以它可以支持 任何现代浏览器。你不必非得使用 Babel 或模块打包器来使用 Redux。

Redux 附加包

多数情况下,你还需要使用 React 绑定库开发者工具

npm install --save react-redux
npm install --save-dev redux-devtools

需要提醒的是,和 Redux 不同,很多 Redux 生态下的包并不提供 UMD 文件,所以为了提升开发体验,我们建议使用像 Webpack 和 Browserify 这样的 CommonJS 模块打包器。

Redux 终极版工具包

Redux本身很小,并且不受限制。我们还有一个名为Redux Toolkit的单独的插件程序,其中包含一些自以为是的默认值,可帮助您更有效地使用Redux。这是编写Redux逻辑的官方推荐方法。

RTK包含有助于简化许多常见用例的实用程序,包括商店设置, 创建reducer和编写不变的更新逻辑,甚至立即创建状态的整个“切片”

无论您是设置您的第一个项目的全新Redux用户,还是想简化现有应用程序的资深用户,Redux Toolkit都可以帮助您改善Redux代码。

创建 React Redux 应用

推荐的方式开始与新的应用程序反应,终极版是通过官方终极版+ JS模板用于创建应用程序做出反应,这需要的优势终极版工具包和应对与反应的组分终极版的集成。

npx create-react-app my-app --template redux

Redux 基本示例

应用程序的整个状态存储在单个商店内的对象树中。更改状态树的唯一方法是发出一个action,一个描述发生了什么的对象。要指定动作如何转换状态树,您可以编写pure reducers。

import { createStore } from 'redux'
/**
 * This is a reducer, a pure function with (state, action) => state signature.
 * It describes how an action transforms the state into the next state.
 *
 * The shape of the state is up to you: it can be a primitive, an array, an object,
 * or even an Immutable.js data structure. The only important part is that you should
 * not mutate the state object, but return a new object if the state changes.
 *
 * In this example, we use a `switch` statement and strings, but you can use a helper that
 * follows a different convention (such as function maps) if it makes sense for your
 * project.
 */
function counter(state = 0, action) {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    case 'DECREMENT':
      return state - 1
    default:
      return state
  }
}

// Create a Redux store holding the state of your app.
// Its API is { subscribe, dispatch, getState }.
let store = createStore(counter)

// You can use subscribe() to update the UI in response to state changes.
// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.
// However it can also be handy to persist the current state in the localStorage.

store.subscribe(() => console.log(store.getState()))

// The only way to mutate the internal state is to dispatch an action.
// The actions can be serialized, logged or stored and later replayed.
store.dispatch({ type: 'INCREMENT' })
// 1
store.dispatch({ type: 'INCREMENT' })
// 2
store.dispatch({ type: 'DECREMENT' })
// 1

您可以直接使用状态对象(称为action)指定要发生的变化,而不是直接改变状态。然后,编写一个称为reducer的特殊函数,以决定每个动作如何转换整个应用程序的状态。

在典型的Redux应用程序中,只有一个具有单个根减少功能的商店。随着您的应用程序的增长,您将根减速器拆分为较小的减速器,分别在状态树的不同部分上运行。这就像React应用程序中只有一个根组件一样,但是它由许多小组件组成。

对于反应用程序而言,这种体系结构似乎有些过头了,但是这种模式的优点在于可以很好地扩展到大型和复杂的应用程序。它还可以启用功能非常强大的开发人员工具,因为可以跟踪每个突变的发生原因。您可以记录用户会话并仅通过重播每个操作来重现它们。