redux-offline 应用程序在第一次加载时闪烁 InitialState 值

redux-offline app flickers the InitialState values on first load

我在我的 React 应用程序中使用 redux-offline,除了页面首次加载时的值 "flicker" 外,一切看起来都不错。在不到一秒钟的时间里,我可以看到存储变量在被持久值替换之前的初始值。

有解决办法吗?

这是我的商店代码:

import { reducer as reduxFormReducer, FormStateMap } from 'redux-form';
import { connectRouter, routerMiddleware, RouterState } from 'connected-react-router';
import { combineReducers, createStore, applyMiddleware, Store, compose } from 'redux';
import { History, createBrowserHistory } from 'history';
import { offline, createOffline } from '@redux-offline/redux-offline';
import offlineConfig from '@redux-offline/redux-offline/lib/defaults';
import thunk from 'redux-thunk';

// state
export interface IAppState {
    readonly form: FormStateMap;
    readonly router: RouterState;
    ...
}

// tslint:disable-next-line:no-empty
export const neverReached = (never: never) => {};

export const history = createBrowserHistory();

const rootReducer = ((history: History) => combineReducers<IAppState>({
    form: reduxFormReducer,
    router: connectRouter(history),
    ...
}))(history);

const { middleware, enhanceReducer, enhanceStore } = createOffline(offlineConfig);

export function configureStore(): Store<IAppState> {
    // This line is suspect, not sure if this is the middleware required
    const store = createStore(
        enhanceReducer(rootReducer), 
        undefined,
        compose(
            applyMiddleware(middleware, routerMiddleware(history), thunk),
            enhanceStore));

    return store;
}

您可以根据 redux-persist REHYDRATE 操作通过更新 redux 状态来跟踪状态何时重新水化。 redux-offline 将操作类型常量重新导出为 PERSIST_REHYDRATE。像下面这样的简单减速器就足够了。

import {PERSIST_REHYDRATE} from '@redux-offline/redux-offline/lib/constants';

function rehydratedReducer = (state = {rehydrated: false}, action) {
  if (action.type === PERSIST_REHYDRATE) {
    return {rehydrated: true};
  }
  return state;
}