【问题标题】:PersistGate causes the app to stop renderingPersistGate 导致应用停止渲染
【发布时间】:2019-06-28 05:24:41
【问题描述】:

我正在使用 Redux 商店和 PersistGate 配置 React Native 应用程序。 Redux 商店已配置并按预期工作,但PersistGate 导致应用程序停止渲染,即使是第一个屏幕。没有PersistGate,应用程序呈现正常。

这里是App.js 代码:

    import React, {Component} from 'react';
    import { Provider } from 'react-redux';
    import { createStore, applyMiddleware } from 'redux';
    import thunk from 'redux-thunk';
    import { persistStore, autoRehydrate } from 'redux-persist';
    import { PersistGate } from 'redux-persist/integration/react';
    import AppNavigator from './AppNavigator';
    import SplashScreen from 'react-native-splash-screen';
    import allReducers from './store/reducers/index';

    const store = createStore(
      allReducers,
      applyMiddleware(thunk),
      //compose(applyMiddleware(thunk), autoRehydrate()),
    );

    // This line makes store persistent.
    const persistor = persistStore(store);

    type Props = {};
    export default class App extends Component<Props> {
      componentDidMount() {
        if (SplashScreen) {
          SplashScreen.hide();
        }
      }
      render() {
        return (
          <Provider store={ store }>
            <PersistGate persistor={persistor}>
                <AppNavigator />
            </PersistGate>
          </Provider>
        );
      }
    }

Reducer 索引文件:

    import {combineReducers} from 'redux';
    import userReducer from './UserReducer';
    const allReducers= combineReducers({
      user: userReducer,
    });
    export default allReducers;

如果我从 App.js 文件中删除 &lt;PersistGate persistor={persistor}&gt; 标记,则应用程序可以正常工作。但是当我使用PersistGate 时,我只看到一个白屏,没有任何崩溃。

我错过了什么导致这个奇怪的输出?

【问题讨论】:

    标签: javascript reactjs react-native redux redux-persist


    【解决方案1】:

    你还需要调用persistReducer函数:

    const persistConfig = {
      key: 'root',
      storage,
    }
    
    const persistedReducer = persistReducer(persistConfig, rootReducer)
    let store = createStore(persistedReducer)
    let persistor = persistStore(store)
    

    他们的文档中的更多信息:https://github.com/rt2zz/redux-persist

    【讨论】:

    • 成功了,但是商店在重新加载后仍然丢失了值
    • 也许检查persistConfig存储选项,我从来没有在React Native中只在React中做过
    • 太棒了,工作!非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2016-12-26
    相关资源
    最近更新 更多