当前位置: 首页 > React学习笔记 > 正文
流量卡

react-redux学习

游魂 发表于2022年6月15日 13:44

React-Redux介绍

为什么要使用 React-Redux 绑定库?

React 和 Redux 是两个独立的库,两者之间职责独立。因此,为了实现在 React 中使用 Redux 进行状态管理 ,就需要一种机制,将这两个独立的库关联在一起。这时候就用到 React-Redux 这个绑定库了。

react-redux.443f85c0

基本使用

react-redux 文档

react-redux 的使用分为两大步:1 全局配置(只需要配置一次) 2 组件接入(获取状态或修改状态)

先看全局配置:

步骤

  1. 安装 react-redux:yarn add react-redux
  2. 从 react-redux 中导入 Provider 组件
  3. 导入创建好的 redux 仓库
  4. 使用 Provider 包裹整个应用
  5. 将导入的 store 设置为 Provider 的 store 属性值

核心代码

src/index.js 中:

// 导入 Provider 组件
import { Provider } from 'react-redux'
// 导入创建好的 store
import store from './store'
const root = ReactDOM.createRoot(document.querySelector('#root'))
root.render(
  <Provider store={store}>
    <App />
  </Provider>
)

获取状态useSelector

import { useSelector } from 'react-redux'

// Redux 中的状态是数值,所以,可以直接返回 state 本身
const count = useSelector(state => state)

// 比如,Redux 中的状态是个对象,就可以:
const list = useSelector(state => state.list)

例:

import { useSelector } from 'react-redux'

const App = () => {
  const count = useSelector(state => state)

  return (
      <div>
        <h1>计数器:{count}</h1>
      <button>数值增加</button>
            <button>数值减少</button>
    </div>
  )
}

分发动作useDispatch

例:

import { useDispatch } from 'react-redux'

const App = () => {
  const dispatch = useDispatch()

  return (
      <div>
        <h1>计数器:{count}</h1>
      {/* 调用 dispatch 分发 action */}
      <button onClick={() => dispatch(increment(2))}>数值增加</button>
            <button onClick={() => dispatch(decrement(5))}>数值减少</button>
    </div>
  )
}

理解 Redux 数据流

ReduxDataFlow.49fa8c39

代码结构

在使用 Redux 进行项目开发时,不会将 action/reducer/store 都放在同一个文件中,而是会进行拆分

可以按照以下结构,来组织 Redux 的代码:

/store        --- 在 src 目录中创建,用于存放 Redux 相关的代码
  /actions    --- 存放所有的 action
  /reducers   --- 存放所有的 reducer
  index.js    --- redux 的入口文件,用来创建 store

ActionType的使用

步骤

  1. 在 store 目录中创建 actionTypes 目录或者 constants 目录,集中处理
  2. 创建常量来存储 action type,并导出
  3. 将项目中用到 action type 的地方替换为这些常量,从而保持项目中 action type 的一致性
// actionTypes 或 constants 目录:

const increment = 'counter/increment'
const decrement = 'counter/decrement'

export { increment, decrement }

// --

// 使用:

// actions/index.js
import * as types from '../acitonTypes'
const increment = payload => ({ type: types.increment, payload })
const decrement = payload => ({ type: types.decrement, payload })

// reducers/index.js
import * as types from '../acitonTypes'
const reducer = (state, action) => {
  switch (action.type) {
    case types.increment:
      return state + 1
    case types.decrement:
      return state - action.payload
    default:
      return state
  }
}

注:额外添加 Action Type 会让项目结构变复杂,此操作可省略。但,domain/action 命名方式强烈推荐!

Reducer的分离与合并

import { combineReducers } from 'redux'

// 计数器案例,状态默认值为:0
const aReducer = (state = 0, action) => {}
// Todos 案例,状态默认值为:[]
const bReducer = (state = [], action) => {}

// 合并多个 reducer 为一个 根reducer
const rootReducer = combineReducers({
  a: aReducer,
  b: bReducer
})

// 创建 store 时,传入 根reducer
const store = createStore(rootReducer)

// 此时,合并后的 redux 状态: { a: 0, b: [] }

redux管理哪些状态

不同状态的处理方式:

  1. 将所有的状态全部放到 redux 中,由 redux 管理
  2. 只将某些状态数据放在 redux 中,其他数据可以放在组件中,比如:
    • 如果一个状态,只在某个组件中使用(比如,表单项的值),推荐:放在组件中
    • 需要放到 redux 中的状态:
      1. 在多个组件中都要使用的数据【涉及组件通讯】
      2. 通过 ajax 请求获取到的接口数据【涉及到请求相关逻辑代码放在哪的问题】
全文完
本文标签: Redux状态管理工具状态管理工具react-reduxactionType
本文标题: react-redux学习
本文链接: https://www.iyouhun.com/m/?post=228

〓 随机文章推荐

共有1449阅 / 1我要评论
  1. 250075083沙发
    了解一下,谢谢!

发表你的评论吧返回顶部

!评论内容需包含中文

请勾选本项再提交评论