使用app.js中的SwitchNavigator将React Native应用程序连接到Redux存储

时间:2019-04-22 11:50:35

标签: reactjs react-native redux react-redux

在index.js的根目录中,这是代码:

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);

和根目录下的App.js:

import React from 'react';
import { createStackNavigator, createSwitchNavigator, createAppContainer } from 'react-navigation';

import AuthLoadingScreen from './src/routes/Splash';
import HomeScreen from "./src/routes/Tabs/Home2";

const AppStack = createStackNavigator({ Home: HomeScreen });
const AuthStack = createStackNavigator({ SignIn: SignInScreen,Forget:ForgetLogin });

const SwitchNavigator = createSwitchNavigator({
  AuthLoading: AuthLoadingScreen,
  App: HomeScreen,
},
{
  initialRouteName: 'AuthLoading',
});

export default createAppContainer(SwitchNavigator);

所以我尝试连接:

步骤1:

在./src根目录中创建3个文件夹

index.js---app.js
                |
                ----src
                      |
                      redux
                          |
                          --------actions
                          |
                           ----reducers
                          |
                           ----store---index.js

第2步:将商店传递到React Native应用

我在商店文件夹中创建index.js:

import { createStore , applyMiddleware , compose} from 'redux';
import {AsyncStorage} from 'react-native';
import thunk from 'redux-thunk';
import { createLogger } from "redux-logger";
import reducers from './../reducers';
import {autoRehydrate, persistStore} from "redux-persist";

const middleware = [ thunk ];
middleware.push(createLogger());

const store = createStore(
    reducers,
    undefined,
    compose(
        applyMiddleware(...middleware),
        autoRehydrate()
    )
);


persistStore(store, {storage: AsyncStorage});

export default store;

现在,我在根目录中更改index.js:

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import { Provider } from 'react-redux';
import store from "./redux/store";


const RNRedux = () => (
  <Provider store = { store }>
    <App />
  </Provider>
)

AppRegistry.registerComponent(appName, () => RNRedux);

现在我想将React Native应用程序连接到Redux存储,但是如何使用SwitchNavigator?

0 个答案:

没有答案