首页 > 解决方案 > 在 redux-persist 中 autoRehydrate 有什么用,为什么它在 v5 中被删除?

问题描述

我在ReduxPersist的 GitHub 页面上找不到任何东西

我有一段我试图理解的代码,因为它被删除了,autoRehydrate我想知道应该如何使用.redux-persist

import { AsyncStorage } from 'react-native';
import { applyMiddleware, createStore } from 'redux';
import { autoRehydrate, persistStore } from 'redux-persist'
import thunk from 'redux-thunk';
import reducers from '../reducers';

const middleWare = [thunk];

const createStoreWithMiddleware = applyMiddleware(...middleWare)(createStore);

 export default configureStore = (onComplete) => {
  const store = autoRehydrate()(createStoreWithMiddleware)(reducers);
  persistStore(store, { storage: AsyncStorage }, onComplete);

  return store;
};

我找到了一些教程,但它只是说这autoRehydrate必须存在,但没有解释它的实际作用。

标签: javascriptreactjsreact-nativereduxredux-persist

解决方案


autoRehydrate意味着调用操作从磁盘persist/REHYDRATE(您之前已持久化)读取持久化状态,可以将其合并回原始状态。

在从 v4 到 v5 的迁移指南中,他们引入了PersistGate

这会延迟应用 UI 的呈现,直到您的持久状态被检索并保存到 redux。

因此,所有的补液动作都将由它在引擎盖下处理。


推荐阅读