在react开发中,如何实现redux存储数据持久化?

更多详细内容,请微信搜索“前端爱好者戳我 查看

在使用redux的时候我们会发现state中的数据一旦刷新页面就会返回初始化状态,vue中的vuex也是如此,无法持久保存vuex中state数据的状态。

原因:数据保存在内存中,刷新页面后,数据重置,所以,状态会返回初始化状态。

解决方案:数据持久化。简单地说,就是把数据保存在本地存储中,当然,需要做好数据的校验保密工作。

一些复杂的重要数据,需要和后端共同校验,比如token。

想要在redux中解决这个问题,我们可以采用数据持久化插件redux-persist

vuex中我们使用的是vuex-persist持久化插件,redux中我们可以使用 redux-persist 插件来完成数据持久化 。

redux-persist 使用

redux-persist 安装:

npm i  redux-persist --save

或者

yarn add  redux-persist 

改造store.js

store.js

import { createStore, combineReducers } from "redux";
import { SidebarCollapsedReducers } from "./reducers/SidebarCollapsedReducers"; // 导入 SidebarCollapsedReducers

const reducer = combineReducers({
  SidebarCollapsedReducers,
});
const store = createStore(reducer);

export default store;

在store.js中引入用redux-persist

// src/redux/store.js 
import { createStore, combineReducers } from "redux";
import { SidebarCollapsedReducers } from "./reducers/SidebarCollapsedReducers"; // 导入 SidebarCollapsedReducers

import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage"; // defaults to localStorage for web

const persistConfig = {
  // 注意这里 persistConfig
  key: "root",
  storage,
  whitelist: ["SidebarCollapsedReducers"], //设置SidebarCollapsedReducers数据持久化, 注意单词的拼写
};

const reducer = combineReducers({
  SidebarCollapsedReducers,
});

const persistedReducer = persistReducer(persistConfig, reducer); 

const store = createStore(persistedReducer);
const persisstore = persistStore(store);
// 导出
export { store, persisstore };


修改组件中引用store部分

原先写法

import store from "./redux/store";

改成

import { store } from "./redux/store";

在主入口文件,index.js中

// /src/index.js

import React from 'react'  
import ReactDOM from 'react-dom/client' 
import ReactReduxRenderComponent from './06-react-redux/app'   
import {store, persisstore} from './06-react-redux/redux/store';
import {Provider} from "react-redux";

import {PersistGate} from 'redux-persist/integration/react'  // 注意这里  

ReactDOM.createRoot(
    document.getElementById('root')
).render
(<Provider store={store}>
    <PersistGate loading={null} persistor={persisstore}>  // 注意这里  
        <ReactReduxRenderComponent></ReactReduxRenderComponent>
    </PersistGate>  // 注意这里  
 </Provider>
);

redux-persist Blacklist & Whitelist

// BLACKLIST
const persistConfig = {
  key: 'root',
  storage: storage,
  blacklist: ['navigation'] // navigation will not be persisted
};

// WHITELIST
const persistConfig = {
  key: 'root',
  storage: storage,
  whitelist: ['navigation'] // only navigation will be persisted
};

最终效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
React Redux实现数据持久化有多种方法,下面是其一种常用的方法: 1. 使用redux-persist库:redux-persist是一个用于实现Redux数据持久化的第三方库。它通过将Redux store数据保存到本地存储(如localStorage或sessionStorage),以便在刷新页面或重新加载应用程序时保持数据的持久性。 首先,安装redux-persist库: ``` npm install redux-persist ``` 然后,在Redux的配置文件,进行redux-persist的配置和初始化: ```javascript import { createStore } from 'redux'; import { persistStore, persistReducer } from 'redux-persist'; import storage from 'redux-persist/lib/storage'; // 默认使用localStorage // 导入你的reducer import rootReducer from './reducers'; // 配置redux-persist const persistConfig = { key: 'root', // 存储的key,默认为root storage, // 使用的存储引擎,默认为localStorage }; const persistedReducer = persistReducer(persistConfig, rootReducer); // 创建store const store = createStore(persistedReducer); const persistor = persistStore(store); export { store, persistor }; ``` 最后,在应用程序的入口文件,使用`PersistGate`组件包裹整个应用程序,并将`persistor`作为其属性: ```javascript import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import { PersistGate } from 'redux-persist/integration/react'; import { store, persistor } from './store'; ReactDOM.render( <Provider store={store}> <PersistGate loading={null} persistor={persistor}> // 应用程序的根组件 </PersistGate> </Provider>, document.getElementById('root') ); ``` 使用以上配置,Redux的状态将会被自动保存到本地存储,并在应用程序重新加载时被恢复。你可以根据需要自行调整配置,例如设置存储引擎、存储的key等。详细的配置和更多高级用法,请参考redux-persist库的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端布道人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值