Redux-persist使用

  redux-persist作用是将store中的数据缓存到浏览器中,减少数据请求,每当白名单中的数据发生变化,才会进行一次更新缓存的操作,并且这个数据缓存是存在localStorage中的,不是会话级别的缓存。

  安装方式两种:npm install --save redux-persist / yarn add redux-persist

  实现方式主要是依靠两个方法:persistStore和persistReducer,使用persistReducer时需要指定persistConfig,这一项就是你需要缓存的数据处理项,它有着黑白名单的处理方式,还需要一个storage的协助:

 1 import {persistStore, persistReducer} from 'redux-persist';
 2 
 3 import storage from 'redux-persist/lib/storage';
 4 
 5 // BLACKLIST
 6 const persistConfig = {
 7   key: 'root', // key是放入localStorage中的key
 8   storage: storage, // storage简单就可以理解成localStorage的功能封装吧,不过有时候由于版本问题,必要在后一个storage上加一个default属性,可以在console中打出来判断是否需要加
 9   blacklist: ['navigation'] // navigation不会被存入缓存中,其他会,适用于少部分数据需要实时更新
10 };
11  
12 // WHITELIST
13 const persistConfig = {
14   key: 'root',
15   storage: storage,
16   whitelist: ['navigation'] // navigation会存入缓存,其他不会存,适用于大多数数据并不会实时从后台拿数据
17 };

  然后在处理reducer时用到persistReducer,一种是直接使用,另一种你可能会使用到combineReducers,接下来就是创建store了,可能会用到中间件,不过此时不要理睬中间件创建store的过程,期间和你之前的创建方式一样,在store创建好的外边,加一句话,然后export里包含persistor就好:

1 const reducers = persistReducer(persistConfig, reducer);
2  
3 const reducers = combineReducers({
4    depReducer: persistReducer(persistConfig, depReducer)
5 });
6 const persistor = persistStore(store);

  最后在ReactDOM.render()的时候引入另一个组件:

 1 import {PersistGate} from 'redux-persist/lib/integration/react';
 2 
 3 ReactDOM.render(
 4   <Provider store={store}>
 5     <PersistGate persistor={persistor}>
 6       <Dep />
 7     </PersistGate>
 8   </Provider>,
 9   document.getElementById('app')
10 );

转载于:https://www.cnblogs.com/ljwk/p/9605444.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值