react中redux数据持续化 ———— redux-persist

通常在浏览器刷新后,vuex或redux这样持续化的工具里面的数据就会丢失,之前小编发过vueX的,今天来教一下react的

1、下载

npm install redux-persist

2、配置store

这是小编的原来的index.js

import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "./modules/counterStore";
import channelReducer from './modules/channelStore'
const store = configureStore({
    reducer: {
        counter: counterReducer,
        channel: channelReducer
    }
})
export default store

添加redux-persist

import { configureStore } from "@reduxjs/toolkit";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage"; // 使用 localStorage 作为持久化存储
import userReducer from "./modules/userStore";
import channelReducer from './modules/channelStore';
import { combineReducers } from "redux";
// 定义持久化配置
const persistConfig = {
  key: "root", // 本地存储的键名
  storage, // 使用的存储引擎,默认为 localStorage
  whitelist: ["user", "channel"] // 需要持久化的 reducer 的名字
};

// 将 reducer 与持久化配置整合
const persistedReducer = persistReducer(
  persistConfig,
  combineReducers({
    user: userReducer,
    channel: channelReducer
  })
);

// 创建 Redux store
const store = configureStore({
  reducer: persistedReducer // 使用整合后的 reducer
});

// 创建持久化的 store 版本
const persistor = persistStore(store);

export { store, persistor };

添加到index.js

初始

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { Provider } from 'react-redux';
import './global.scss'
import store from './store/index';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <Provider store={store}>
      <App />
  </Provider>
);

修改后

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { Provider } from 'react-redux';
import './global.scss'
import { PersistGate } from 'redux-persist/integration/react'; // 引入 PersistGate
import { store, persistor } from './store/index';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      <App />
    </PersistGate>
  </Provider>
);


相关推荐

  1. reactredux数据持续 ———— redux-persist

    2024-04-12 06:22:05       39 阅读
  2. react项目使用reduxreduxjs/toolkit

    2024-04-12 06:22:05       37 阅读
  3. React Redux使用@reduxjs/toolkit的hooks

    2024-04-12 06:22:05       25 阅读
  4. Redux,react-redux,dva,RTK

    2024-04-12 06:22:05       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 06:22:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 06:22:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 06:22:05       87 阅读
  4. Python语言-面向对象

    2024-04-12 06:22:05       96 阅读

热门阅读

  1. 微服务中无服务状态和有服务状态分析

    2024-04-12 06:22:05       38 阅读
  2. 测试需求分析

    2024-04-12 06:22:05       38 阅读
  3. html讲义

    2024-04-12 06:22:05       39 阅读
  4. node.js-fs模块

    2024-04-12 06:22:05       36 阅读
  5. stack类介绍

    2024-04-12 06:22:05       38 阅读
  6. ansible使用shell模块的环境变量问题

    2024-04-12 06:22:05       34 阅读
  7. g++ 13.2.0 编译 C++模块

    2024-04-12 06:22:05       42 阅读