首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >redux-离线应用程序在第一次加载时闪烁InitialState值

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

Stack Overflow用户
提问于 2019-11-12 11:02:19
回答 1查看 273关注 0票数 0

我在我的React应用程序中使用了redux-offline,除了页面第一次加载时值“闪烁”之外,一切看起来都很好。在不到一秒钟的时间内,我可以看到存储变量的初始值,然后将其替换为持久化的值。

有什么办法可以解决这个问题吗?

这是我的商店代码:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-14 06:46:12

您可以通过基于redux-persist REHYDRATE操作更新redux状态来跟踪状态恢复的时间。redux-offline将动作类型常量重新导出为PERSIST_REHYDRATE。像下面这样的一个简单的reducer应该足够了。

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58811388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档