redux-persist管理redux, 解决刷新react-redux数据丢失!

  在我们实际开发react项目中可能有某些需求需要持久化报错redux里面的数据,比如登录状态,这时候就可以用到redux-persist 这款插件解决这个问题,它的原理是使用sessionstorage来持久化数据。

1:安装redux-persist这款插件

  

npm install redux-persist --save

//

yarn add redux-persist

2:在redux文件夹下的index.js添加

import { createStore} from 'redux';
import reducer from './reducer';
import {persistStore, persistReducer} from 'redux-persist';
import storageSession from 'redux-persist/lib/storage/session';
 const storageConfig = {
        key: 'root', // 必须有的
        storage:storageSession, // 缓存机制
        blacklist: ['name','age'] // reducer 里不持久化的数据,除此外均为持久化数据
}
const myPersistReducer = persistReducer(storageConfig, reducer);
const store = createStore(myPersistReducer);

export const persistor = persistStore(store)
export default store;

3:在项目根文件下index.js下

import React from 'react';
import ReactDOM from 'react-dom';
// import App from './App';
import {Provider} from 'react-redux';
import store from './store';
import {persistor} from './store';
import TodoList from './TodoList';
import * as serviceWorker from './serviceWorker';
import {PersistGate} from 'redux-persist/lib/integration/react';
ReactDOM.render(
          <Provider store={store}>
            <PersistGate loading={null} persistor={persistor}>
                <TodoList/>
            </PersistGate>
          </Provider>
          , document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

 

以上就可以解决redux数据刷新丢失的情况!!!

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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 的状态就是同步的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值