基于 Redux 状态管理‖青训营笔记

162 阅读5分钟

前言

组件嵌套多层时,可能需要把父级的状态一层一层向下传递,这样在管理和使用上极其不便。

Redux 是 JS 的状态容器,提供可预测化的状态管理。在 React 中使用 Redux,可以把所有的 state 集中到组件顶部,能灵活地将所有 state 分发给所有的组件。

 

3.1 Redux 使用

Redux 本身不依赖于任何库,可以与任何 UI 层框架搭配使用,大小也只有 2KB。

 

核心概念

  1)store:一个数据容器,用来管理和保存项目的 state,整个应用只能有一个 store。

  2)state:一个对象,在 state 中存储相应的数据,需要时通过 store 提供的方法获取。

  3)action:一个通知命令,视图层发起的一个操作,对 state 进行修改。

 

3.1.1 action、createStore 和 reducer 函数

action 本质上是一个 JS 普通对象,必须包含一个字符串类型的 type 属性,代表要对 state 做何种操作。最终 action 通过 store 传入 reducer 函数,完成对 state的修改。

Redux Toolkit 是官方推荐的用于编写 Redux 逻辑的方式,它包含了 Redux Core,还包含了对创建 Redux 应用非常必要的包和功能。

安装 Redux Core:npm install redux

安装 Redux Toolkit:npm install @reduxjs/toolkit

 

先看 Redux 中的核心方法 createStore,用于创建项目中的 store。

1      /** 2 * This is a reducer - a function that takes a current state value and an 3 * action object describing "what happened", and returns a new state value. 4 * A reducer's function signature is: (state, action) => newState 5 * 6 * The Redux state should contain only plain JS objects, arrays, and primitives. 7 * The root state value is usually an object. It's important that you should 8 * not mutate the state object, but return a new object if the state changes. 9 * 10 * You can use any conditional logic you want in a reducer. In this example, 11 * we use a switch statement, but it's not required. 12 */ 13 function counterReducer(state = { value: 0 }, action) { 14 switch (action.type) { 15 case 'counter/incremented': 16 return { value: state.value + 1 } 17 case 'counter/decremented': 18 return { value: state.value - 1 } 19 default: 20 return state 21 } 22 } 23 24 // Create a Redux store holding the state of your app. 25 // Its API is { subscribe, dispatch, getState }. 26 let store = createStore(counterReducer)

createStore 创建 store 时需要传入 reducer 函数。reducer 是一个函数,它接受当前状态值和描述“发生了什么”的操作对象,并返回一个新的状态值。

在 Redux 中,所有的数据都会保存在同一个 state 对象中。

 

3.1.2 store

store 的三个常用方法。

  1)getState:用于获取 state。

  2)dispatch(action):发起一个 action。

  3)subscribe(listener):注册一个监听器监听 state 发生的变化。返回一个注销监听器的方法,用于取消监听器。

 

Redux 基本流程:基于 reducer 创建 store,从 store 中获取 state 传递给视图,当视图被操作时,通过 dispatch 发起一个 action,store 接到 action 后,把 state 和 action 传递给 reducer,reducer 更新 state 并把新的 state 传给视图进行更新。

下面是纯 Redux 的测试代码:

1 2 3 4 React Demo 5 6 7 8 9 10

11

12 Watch my color. 13 RED 14 GREEN 15 TOGGLE 16

17
18 64 65

 

 

3.2 React-Redux

在 React 中直接使用 Redux 特别麻烦,需要一层一层传递 store。

Redux 官方提供了一个 React 绑定库 - React-Redux。

 

3.2.1 安装与配置

命令:npm install react-redux,需要注意的是,React-Redux 是 Redux 的官方针对 React 开发的扩展库,默认没有在 React 项目中安装,需要手动来安装。React-Redux 是依赖于redux,所以 Redux 也 必须安装。

安装了 React-Redux 之后,就可以把数据和视图进行分离。

  1)第一步,创建 store。

  2)通过 React-Redux 把 store 关联到项目中。

    React-Redux 提供了组件 Provider,它的作用是向后代子孙传递信息。Provider 应该在整个项目的最外层。

    

3.2.2 connect

React-Redux 提供了 connect 方法用于接收 Provider 传递下来的 store 中的 state 和 dispatch。

下面是一个使用了 connect 的例子。

1 2 3 4 React Demo 5 6 7 8 9 10 11 12 13 14

15 42 {count} 43 <button onClick={() => { 44 dispatch({type: "COUNT_PLUS"}); 45 }}>+ 46 47 ); 48 } 49 } 50 51 App = ReactRedux.connect(state => state)(App); 52 53 ReactDOM.createRoot(document.querySelector('#root')) 54 .render(<ReactRedux.Provider store={store}> 55 56 </ReactRedux.Provider>); 57 58 59

 

3.2.3 Hooks

React-Redux 7.X 新增了 Hooks。

  1)const state = useSelector(state => state)

  useSelector 接收一个回调函数,这个函数会被调用,并且 store 中的 state 会被传递给这个函数,返回值是想要的数据,必须有返回值。

  2)const dispatch = useDispatch()

  返回值为 Redux 的 dispatch 方法。

  3) const store = useStore()

  返回 Redux 的 store。

 

3.4 reducer 拆分与合并

combineReducers(rootReducer)。

 

3.5 redux-thunk 中间件

dispatch 一个 action 之后,到达 reducer 之前,进行一些额外的操作,就需要用到 middleware。你可以利用 Redux middleware 来进行日志记录、创建崩溃报告、调用异步接口或者路由等等。 换言之,中间件都是对 store.dispatch() 的增强。

redux-thunk 主要用于解决项目中的异步请求。

  1)安装:npm install redux-thunk。

  2)在创建 store 时,引入中间件。Redux 中提供了 applyMiddleware(...middlewares)方法,把方法的返回值传递给 createStore。

  3)添加完成后可以利用 thunk 做中间处理。使用了 thunk 之后,dispatch 可以接收两种不同类型的参数。参数类型是对象时,不会经过中间件处理而是直接把 action 发送到 reducer。参数类型是函数时,会把 dispatch 和 getState 作为参数传递给该函数,在函数中进行中间处理,处理之后调用 dispatch 更新 state。 (摘自自认为有用的文章)