入门 Redux Toolkit

目标

Redux Toolkit 包旨在成为编写 Redux 逻辑的标准方式。它最初是为了解决 Redux 的三个常见问题而创建的:

  • "配置 Redux store 过于复杂"
  • "我需要添加许多包才能让 Redux 做任何事情"
  • "Redux 需要太多样板代码"

我们无法解决所有问题,但秉承 create-react-app 的精神,我们可以尝试提供一些工具来抽象设置过程并处理最常见的场景,以及包含一些有用的实用程序,让用户简化他们的应用程序代码。

Redux Toolkit 还包含一个强大的数据获取和缓存功能,我们将其称为"RTK Query"。它作为单独的一组入口点包含在包中。它是可选的,但可以消除手动编写数据获取逻辑的需要。

这些工具应该对所有 Redux 用户都很有益处。无论你是全新的 Redux 用户设置你的第一个项目,还是想要简化现有应用程序的经验丰富用户,Redux Toolkit 都可以帮助你改进你的 Redux 代码。

安装

为了确保可以使用React Hooks, Redux Toolkit 8.x 需要依赖于 React 16.8.3 或更高版本/React Native 0.59 或更高版本

使用 Create React App

用 React 和 Redux 构建新应用的推荐方式是使用 Create React App 的官方 Redux+JS 或 Redux+TS 模板。这些模板利用了 Redux Toolkit 和它与 React 组件的集成,因此无需手动配置。

arduino 复制代码
# Redux + Plain JS template
npx create-react-app my-app --template redux

# Redux + TypeScript template
npx create-react-app my-app --template redux-typescript

对于现存的 React APP

为了能在你的 React APP 中使用 Redux Toolkit,需先安装依赖:

ini 复制代码
# If you use npm:
npm install react-redux

# Or if you use Yarn:
yarn add react-redux

你还需要安装 Redux 并在你的应用程序中设置一个 Redux 存储。

React-Redux v8 是用 TypeScript 编写的,故所有的类型都已包含在内

API 概览

Provider

Redux Toolkit 包括 Provider 组件, 该组件为你的 app 提供了可用的 Redux Store

jsx 复制代码
import React from 'react'  
import ReactDOM from 'react-dom/client'  
  
import { Provider } from 'react-redux'  
import store from './store'  
  
import App from './App'  
  
// As of React 18  
const root = ReactDOM.createRoot(document.getElementById('root'))  
root.render(  
  <Provider store={store}>  
  <App />  
  </Provider>  
)

Hooks

Redux Toolkit 提供了一对自定义 React hook,允许你的 React 组件与 Redux store 进行交互。

useSelector 从 store state 读取值并订阅更新,而 useDispatch 返回 store 的调度方法以让你分发 actions。

jsx 复制代码
import React from 'react'
import { useSelector, useDispatch } from 'react-redux'
import {
  decrement,
  increment,
  incrementByAmount,
  incrementAsync,
  selectCount,
} from './counterSlice'
import styles from './Counter.module.css'

export function Counter() {
  const count = useSelector(selectCount)
  const dispatch = useDispatch()

  return (
    <div>
      <div className={styles.row}>
        <button
          className={styles.button}
          aria-label="Increment value"
          onClick={() => dispatch(increment())}
        >
          +
        </button>
        <span className={styles.value}>{count}</span>
        <button
          className={styles.button}
          aria-label="Decrement value"
          onClick={() => dispatch(decrement())}
        >
          -
        </button>
      </div>
      {/* omit additional rendering output here */}
    </div>
  )
}
相关推荐
云水一下1 小时前
从零开始!VMware安装Fedora Workstation 44桌面系统完整教程
前端
小码哥_常2 小时前
安卓黑科技:实现多平台商品详情页一键跳转APP
前端
killerbasd2 小时前
还是迷茫 5.3
前端·react.js·前端框架
不会敲代码13 小时前
TCP/IP 与前端性能:从数据包到首次渲染的底层逻辑
前端·tcp/ip
kyriewen3 小时前
奥特曼借GPT-5.5干杯,而你的Copilot正按Token收钱
前端·github·openai
AC赳赳老秦3 小时前
投标合规提效:用 OpenClaw 实现标书 / 合同自动审核、关键词校验、格式优化,降低废标风险
开发语言·前端·python·eclipse·emacs·deepseek·openclaw
kyriewen3 小时前
代码写成一锅粥?3个设计模式让你的项目“起死回生”
前端·javascript·设计模式
千寻girling4 小时前
《 Git 详细教程 》
前端·后端·面试
之歆5 小时前
DAY08_CSS浮动与行内块布局实战指南(下)
前端·css
yqcoder5 小时前
CSS Position 全解析:5 种定位模式详解
前端·css