【问题标题】:React native initalRouteName not working with Stack Navigation反应本机 initalRouteName 不适用于堆栈导航
【发布时间】:2018-05-16 12:47:17
【问题描述】:

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

Index.js

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

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);

SignUp.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);

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

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 屏幕。请帮忙

【问题讨论】:

  • 您的路线中是否需要“路径”?尝试摆脱它,看看是否有帮助。
  • 我删除了路径。我不需要它们。问题是由于在组件渲染方法中执行 this.props.navigate

标签: react-native react-redux stack-navigator


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2022-10-16
    • 1970-01-01
    • 2019-12-16
    • 2020-07-10
    • 1970-01-01
    • 2020-01-28
    相关资源
    最近更新 更多