3

我正在使用 redux-persist,我正在尝试渲染一个屏幕,将其传递给 PersistGate 的加载道具。

我做了一些研究,发现我应该将 REHYDRATE 发送到减速器,但这也不起作用。

也许我没有很好地配置我的减速器?我还希望能够将加载道具设置为 null 以避免在应用程序渲染之前闪屏,但结果与将组件传递给渲染相同。

这是我的 index.js 代码

import App from './App';
import React from 'react';
import { Provider } from 'react-redux';
import { AppRegistry } from 'react-native';
import { PersistGate } from 'redux-persist/integration/react';
import { SplashScreen } from './src/screens/SplashScreen';
import configureStore from './src/store/configureStore';

const store = configureStore();
const persistor = configureStore();

const RNRedux = () => (
    <Provider store={store}>
        <PersistGate loading={<SplashScreen/>} persistor={persistor}>
            <App />
        </PersistGate>
    </Provider>
);

componentDidMount = () => {
    this.persistor.dispatch({ type: REHYDRATE });
};

AppRegistry.registerComponent('Sharryapp', () => RNRedux);

这就是我的 configureStore 文件:

import { createStore, combineReducers, applyMiddleware} from 'redux';
import ServerReducer from './reducers/ServerReducer';
import InviteReducer from './reducers/InviteReducer';
import { persistStore, persistReducer } from 'redux-persist';
import thunk from 'redux-thunk';
import storage from 'redux-persist/lib/storage';

const rootReducer = combineReducers({
    server: ServerReducer,
    invite: InviteReducer,
});

const persistConfig = {
    key: 'root',
    debug: true,
    storage, 
}

const persistedReducer = persistReducer(persistConfig, rootReducer);

const store = createStore(persistedReducer,applyMiddleware(thunk));

const persistor = persistStore(store);

export default configureStore = () => {
    return ( store, persistor );
};

4

1 回答 1

5

我不确定你为什么将你的商店和持久化器包装在 configureStore 函数中。而是分别导入:

export const store = createStore(persistedReducer,applyMiddleware(thunk));
export const persistor = persistStore(store);

并将它们导入您想要的文件中:

import {store, persistor} from './src/store/configureStore';

我还注意到您的 createStore 调用是错误的,因为增强器是作为第三个参数传递的。将其更改为:

const store = createStore(persistedReducer, undefined, applyMiddleware(thunk));

那应该这样做。

此外,您不需要调度 rehydration 操作,因为它会在应用程序启动时自动发生。

于 2018-08-04T19:39:44.507 回答