7

我正在使用 react-native-navigation 和 redux 进行状态管理。我将每个组件注册为 WrappedComponent,将其连接到 redux 存储。这非常有效,并且与官方 react-native-navigation 文档中的 atoami 示例代码非常相似:https ://wix.github.io/react-native-navigation/#/docs/showcases

import { Provider } from "react-redux";
import store from "./config/store";
...
function WrappedComponent(Component) {
  return function inject(props) {
    const EnhancedComponent = () => (
      <Provider store={store}>
        <Component {...props} />
      </Provider>
    );

    return <EnhancedComponent />;
  };
}

export function registerScreens() {
  Navigation.registerComponent("Initializing", () =>
    WrappedComponent(InitializingScreen)
  );
  Navigation.registerComponent("SignIn", () => WrappedComponent(SignInScreen));
...
}

商店对象为:

import { createStore, compose, applyMiddleware } from "redux";
import thunk from "redux-thunk";

import reducers from "../reducers";

const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

export default createStore(
  reducers,
  composeEnhancer(applyMiddleware(thunk))
);

但是,我找不到为那些包装的组件设置 redux 持久性的方法。我不想在WrappedComponent函数中执行它,因为它会被每个单独的组件调用。我也找不到关于此的明确文档。

我想我也可以使用 AsyncStorage,但更愿意将它与 Redux-persist 一起使用。有人知道该怎么做吗?

4

2 回答 2

4

这就是我处理 redux、redux 和 wix v2 react-native-navigation 的方式

在你的store.js

import {createStore,applyMiddleware} from "redux";
import rootReducer from './reducers/RootReducer';
import thunk from 'redux-thunk';
import {persistStore, persistReducer} from 'redux-persist';
import {compact} from "lodash";
import storage from 'redux-persist/lib/storage';


const persistConfig = {
    key: 'app',
    storage,
};
const persistedReducer = persistReducer(persistConfig, rootReducer);

const middleware =compact([
    thunk.withExtraArgument()
]);


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

然后在您navigation.js或您基本上注册屏幕的地方

import React from "react";
import {Navigation} from "react-native-navigation";
import {Provider} from 'react-redux';
import {PersistGate} from 'redux-persist/integration/react'
import {store, persistor} from "./config/store"; //Check this line

function WrappedComponent(Component) {
  return function inject(props) {
    const EnhancedComponent = () => (
     <Provider store={store}>
         <PersistGate loading={null} persistor={persistor}>
             <Component {...props}/>
         </PersistGate>
      </Provider>
    );
    return <EnhancedComponent />;
  };
}

// Then your normal registration
export function registerScreens() {
  Navigation.registerComponent("Initializing", () =>
    WrappedComponent(InitializingScreen)
  );
  Navigation.registerComponent("SignIn", () => WrappedComponent(SignInScreen));
...
}
于 2019-06-15T16:16:14.550 回答
0
import {persistStore} from 'redux-persist';
Navigation.events().registerAppLaunchedListener(() => {
 persistStore(store,null,()=>{
   setRoot();
});
});

你可以像这样使用 react-native-navigation 添加 Redux-persist。

于 2019-06-14T14:40:27.543 回答