一. 安装依赖
安装 redux
、react-redux
、@reduxjs/toolkit
。
持久化存储还需要安装 redux-persist
、@react-native-async-storage/async-storage
。
| npm install redux react-redux @reduxjs/toolkit redux-persist @react-native-async-storage/async-storage |
二. 创建store
1. 首先创建store文件夹,存放所有redux相关文件。
2. 创建slices文件夹,存放所有拆分的slice文件。
这里举例,假设有两个slice,slices/userSlice.js
和slices/otherSlice.js
分别存放用户数据和其它数据。实际根据需求创建相应的slice。
代码如下:
| import {createSlice} from '@reduxjs/toolkit'; |
| |
| const userSlice = createSlice({ |
| name: 'user', |
| initialState: { |
| |
| userData: {}, |
| }, |
| reducers: { |
| setUserData: (state, action) => { |
| state.userData = action.payload; |
| }, |
| }, |
| }); |
| |
| export const {setUserData} = userSlice.actions; |
| |
| export default userSlice.reducer; |
点击查看代码
| import {createSlice} from '@reduxjs/toolkit'; |
| |
| const otherSlice = createSlice({ |
| name: 'other', |
| initialState: { |
| |
| }, |
| reducers: { |
| |
| |
| }, |
| }); |
| |
| export const { |
| |
| } = otherSlice.actions; |
| |
| export default otherSlice.reducer; |
3. 创建reducers.js作为根reducer
| import {combineReducers} from '@reduxjs/toolkit'; |
| import userReducer from './slices/userSlice'; |
| import otherSlice from './slices/otherSlice'; |
| |
| |
| const rootReducer = combineReducers({ |
| user: userReducer, |
| other: otherSlice, |
| |
| }); |
| |
| export default rootReducer; |
4. 创建index.js作为Redux Store:
①一般写法
| import { configureStore } from '@reduxjs/toolkit'; |
| import rootReducer from './reducers'; |
| |
| const store = configureStore({ |
| reducer: rootReducer, |
| }); |
| |
| export default store; |
②使用持久化存储的写法
| import {configureStore} from '@reduxjs/toolkit'; |
| import {persistReducer, persistStore} from 'redux-persist'; |
| import AsyncStorage from '@react-native-async-storage/async-storage'; |
| import rootReducer from './reducers'; |
| |
| const persistConfig = { |
| key: 'root', |
| storage: AsyncStorage, |
| whitelist: ['user'], |
| }; |
| |
| const persistedReducer = persistReducer(persistConfig, rootReducer); |
| |
| export const store = configureStore({ |
| reducer: persistedReducer, |
| middleware: () => [], |
| }); |
| export const persistor = persistStore(store); |
三、在根组件注册
| import React from 'react'; |
| import { Provider } from 'react-redux'; |
| import { PersistGate } from 'redux-persist/integration/react'; |
| import { store, persistor } from './store'; |
| import MyComponent from './MyComponent'; |
| import {ToastProvider} from 'react-native-toast-notifications'; |
| import { NavigationContainer } from '@react-navigation/native'; |
| |
| export default function Main() { |
| return ( |
| <Provider store={store}> |
| <PersistGate loading={null} persistor={persistor}> |
| <ToastProvider> |
| <NavigationContainer> |
| <MyComponent /> |
| {/* 在这里可以渲染其他需要访问store的组件 */} |
| </NavigationContainer> |
| </ToastProvider> |
| </PersistGate> |
| </Provider> |
| ); |
| } |
首先从redux-persist中导入PersistGate,然后使用PersistGate组件包装整个应用程序的渲染。我们将persistor作为PersistGate的persistor属性值传递。loading属性用于指定在持久化恢复之前显示的加载UI。
四、组件中引入并使用
| import React from 'react'; |
| import { useSelector, useDispatch } from 'react-redux'; |
| import { } from './slices/userSlice'; |
| import { } from './slices/otherSlice'; |
| |
| const MyComponent = () => { |
| const userData = useSelector((state) => state.user); |
| const otherData = useSelector((state) => state.other); |
| const dispatch = useDispatch(); |
| |
| |
| const updateUser = () => { |
| dispatch(); |
| }; |
| |
| const updateOtherData = () => { |
| dispatch(); |
| }; |
| |
| |
| return ( |
| <div> |
| <h1>User Data: {userData}</h1> |
| <button onClick={updateUser}>Update User Data</button> |
| |
| <h1>Other Data: {otherData}</h1> |
| <button onClick={updateOtherData}>Update Other Data</button> |
| </div> |
| ); |
| }; |
| |
| export default MyComponent; |
五、相关链接
redux 原版文档
redux-toolkit 原版文档
redux 文档中文版
redux-toolkit 文档中文版
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程