整个 redux 采用功能进行划分数据数据流, 所以创建目录一般在 src/features/功能名称 下, 然后将 reducer 导出到 store 中进行管理.
安装依赖
在新项目中安装依赖
| 1 | npx create-react-app my-app --template redux-typescript | 
已有项目安装依赖
| 1 | yarn add react-redux @reduxjs/toolkit | 
修改主页内容支持 redux
修改 index.html 增加内容
| 1 | import { Provider } from "react-redux"; | 
| 1 | <Provider store={store}> | 
构建数据管理模块 Slice
src/features/light/lightSlice.ts
| 1 | import { createSlice } from "@reduxjs/toolkit"; | 
与数据管理器交互
src/features/light/Light.tsx
注意: 和 js 不同, ts 在包含 React 组件的文件要以 tsx 作为文件名
| 1 | import React, { useState } from "react"; | 
构建数据中心 store
src/store.ts
假设功能名称为 light
| 1 | import { configureStore } from "@reduxjs/toolkit"; | 
src/hook.ts
| 1 | import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux"; | 
 
         
        