1

我是 react-native 的新手,我正在尝试使用 StackNavigation 和带有欢迎和注册屏幕的 react-redux 来实现一个简单的应用程序。我已经使用 StackNavigation 配置了两个屏幕,但由于某些原因,应用程序启动时仅弹出 SignUp 屏幕。以下是我的文件:

索引.js

import { AppRegistry } from 'react-native';
import App from './App';
AppRegistry.registerComponent('MyApp', () => App);

应用程序.js

import React, { Component } from 'react';
import { Provider, connect } from "react-redux";
import { addNavigationHelpers } from "react-navigation";
import StackNavConfig from "./js/config/routes";
import getStore from "./js/store";

const AppNavigator = StackNavConfig;

const initialState = AppNavigator.router.getActionForPathAndParams('Welcome');

const navReducer = (state = initialState, action) => {
    const newState = AppNavigator.router.getStateForAction(action, state);
    return newState || state;
};

const AppWithNavigationState = connect(state => ({
    nav: state.nav,
}))(({ dispatch, nav }) => (
    <AppNavigator navigation={addNavigationHelpers({ dispatch, state: nav })} />
));

const store = getStore(navReducer);

class App extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <AppWithNavigationState />
      </Provider>
    );
  }
}

export default App;

js/config/routes.js

import Welcome from "../components/Welcome/view/Welcome";
import SignUp from "../components/SignUp/view/SignUp";
import { StackNavigator } from "react-navigation";

const Routes = {
    Welcome: { screen: Welcome , path: ''},
    SignUp: { screen: SignUp , path : '/signup'},
};

const RoutesConfig = {
    initialRouteName: 'Welcome',
    headerMode: 'none',
};

export default StackNavConfig = StackNavigator(Routes, RoutesConfig);

store.js

import { createStore, applyMiddleware } from "redux";

import thunk from "redux-thunk";
import getRootReducer from "./reducers/index";

export default function getStore(navReducer) {
    const store = createStore(
        getRootReducer(navReducer),
        undefined,
        applyMiddleware(thunk)
    );

    return store;
}

以下是我的组件 Welcome.js

import React from 'react';
import {  
  View, 
  Image} from 'react-native'; 
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as welcomeActions from "../actions/WelcomeActions";
import { welcomeStyles } from '../styles/WelcomeStyles';

class Welcome extends React.Component {

  constructor(){
    super();
    this.state = {  };
  }

  render(){
    return (
      <View style = {welcomeStyles.mainContainer}>
         <Text>Welcome</Text>
      </View>      
    ); 
  }
}    

export default connect(
  state => ({

  }),
  dispatch => bindActionCreators(welcomeActions, dispatch)
)(Welcome);

注册.js

import React from 'react';
import {  
  View, 
  Image} from 'react-native'; 
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as welcomeActions from "../actions/SignUpActions";
import { signUpStyles } from '../styles/SignUpStyles';

class Welcome extends React.Component {

  constructor(){
    super();
    this.state = {  };
  }

  render(){
    return (
      <View style = {signUpStyles.mainContainer}>
         <Text>SignUp</Text>
      </View>      
    ); 
  }
}    

export default connect(
  state => ({

  }),
  dispatch => bindActionCreators(signUpActions, dispatch)
)(SignUp);

我的每个组件也有动作和减速器文件。但它们现在是空白的,因为我还没有实现 redux 部分。我正在组合减速器,如下所示。

import { combineReducers } from "redux";
import welcomeReducer from "../components/Welcome/reducers/WelcomeReducer";
import signUpReducer from "../components/SignUp/reducers/SignUpReducer";

export default function getRootReducer(navReducer) {
    return combineReducers({
        nav: navReducer,
        welcomeReducer : welcomeReducer,
        signUpReducer : signUpReducer,
    });
}

如前所述,即使在我的 routes.js 中将 initialRouteName 设置为 Welcome 之后,每次启动应用程序时都会首先出现 SignUp 屏幕。请帮忙

4

1 回答 1

1

我发现了问题所在。我在渲染函数中错误地调用了 this.props.navigate,导致导航到不同的屏幕。

于 2017-12-06T02:31:03.360 回答