Redux 状态持久化之 redux-persist 使用示例

在这里插入图片描述
同vuex一样,redux中的状态会在刷新浏览器后状态又恢复到初始状态,有些数据想在浏览器刷新后仍然是在最新的状态,不会丢失,就需要借助一些插件实现。本文通过 redux-persist 插件来实现Redux状态的持久化。

下面使用 redux-persist 插件的示例代码:

  1. 首先需要安装 redux-persist 依赖:
npm install redux-persist
  1. 在 Redux store 的配置文件中 (比如 store.js),引入 redux-persist 相关的模块和配置:
import { createStore } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // 默认使用 localStorage 作为存储介质

import rootReducer from './reducers'; // 你的 Redux 根Reducer

// 配置 redux-persist 的持久化选项
const persistConfig = {
  key: 'root', // 存储在 localStorage 中的键名
  storage, // 使用 localStorage 作为存储介质
  whitelist: ['yourReducerKey'] // 指定需要持久化的 reducer 键名
};

// 创建一个持久化的 reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);

// 创建 Redux store
const store = createStore(persistedReducer);

// 创建持久化存储
const persistor = persistStore(store);

export { store, persistor };
  1. 在 React 应用的入口文件中 (比如 index.js),使用 PersistGate 组件包裹根组件,以便在 Redux store 重新加载时恢复持久化的状态:
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';
import App from './App';

ReactDOM.render(
  <Provider store={store}>
    <PersistGate loading={<div>Loading...</div>} persistor={persistor}>
      <App />
    </PersistGate>
  </Provider>,
  document.getElementById('root')
);

在上面的示例代码中:

  • 我们使用 persistReducer 函数包装了 Redux 的根 reducer,并配置了持久化选项,包括存储介质和需要持久化的 reducer 键名。
  • 然后使用 persistStore 函数创建了一个持久化存储实例 persistor
  • 在 React 应用的入口文件中,我们使用 PersistGate 组件包裹了根组件,并传递了 persistor 实例。当 Redux store 重新加载时,PersistGate 会先从存储介质中恢复持久化的状态,然后再渲染根组件。

通过这种方式,你就可以在 Redux 应用中使用 redux-persist 实现状态的持久化存储和恢复了。你可以根据具体需求自定义持久化选项,比如使用不同的存储介质、指定不同的持久化键名等。

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 的状态管理库 Pinia 和 Redux持久化redux-persist 的结合使用可以实现在 Vuex 使用持久化存储功能。 首先,安装依赖: ```shell npm install pinia redux-persist ``` 然后在 `src/store/index.ts` 引入 Pinia 和 redux-persist: ```typescript import { createPinia } from 'pinia' import { persist } from 'pinia-plugin-persist' import { createStore } from 'redux' import { persistStore, persistReducer } from 'redux-persist' import storage from 'redux-persist/lib/storage' const pinia = createPinia() // 定义 Pinia 插件,使用 redux-persist 进行持久化存储 pinia.use( persist({ // 持久化存储的 key key: 'pinia', // 持久化存储的引擎,默认使用 localStorage storage, // 将 Pinia 的状态转换为 Redux状态 reducer: (state: any) => state.value, // 将 Redux状态转换为 Pinia 的状态 restoreState: (reduxState: any) => ({ value: reduxState }), }) ) // 定义 Redux 的 reducer const reducer = (state = 0, action: any) => { switch (action.type) { case 'INCREMENT': return state + 1 case 'DECREMENT': return state - 1 default: return state } } // 创建 Redux 的 store const store = createStore( persistReducer({ key: 'redux', storage }, reducer), undefined, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() ) // 持久化存储 Redux 的 store const persistor = persistStore(store) export { pinia, store, persistor } ``` Pinia 插件使用 redux-persist 的 `persistReducer` 方法将 Pinia 的状态转换为 Redux状态,并进行持久化存储;`restoreState` 方法则将 Redux状态转换为 Pinia 的状态。 在应用使用 Pinia 时,可以直接使用 Pinia 的 API 进行状态管理,也可以通过 Redux 的 API 进行状态管理。例如,在 `src/App.vue` : ```vue <template> <div> <div>Pinia: {{ $pinia.state.value }}</div> <div>Redux: {{ $store.getState() }}</div> <button @click="$pinia.state.value++">Pinia +</button> <button @click="$pinia.state.value--">Pinia -</button> <button @click="$store.dispatch({ type: 'INCREMENT' })">Redux +</button> <button @click="$store.dispatch({ type: 'DECREMENT' })">Redux -</button> </div> </template> <script setup> import { useStore } from 'vuex' import { usePinia } from 'pinia' import { store, persistor } from './store' // 注册 Pinia 的 store const pinia = usePinia() pinia.useStore(store) // 注册 Redux 的 store const vuexStore = useStore() vuexStore.replaceState(persistor.getState()) store.subscribe(() => { vuexStore.replaceState(persistor.getState()) }) </script> ``` 在应用同时使用 Pinia 和 Redux 时,需要注意 Pinia 和 Redux状态同步。在上面的例子,Pinia 和 Redux状态都被持久化存储,因此在应用重新加载时,需要将 Redux状态持久化存储恢复,并将其转换为 Pinia 的状态。在 `script setup` ,通过 `useStore` 获取 Vuex 的 store,并使用 `replaceState` 方法将 Redux状态设置为 Vuex 的状态;`store.subscribe` 方法监听 Redux状态变化,并在变化时将 Redux状态设置为 Vuex 的状态。这样,在应用使用 Pinia 的 API 进行状态管理时,Pinia 和 Redux状态就是同步的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值