Akshay Barge
Akshay Barge

Reputation: 113

React native initalRouteName not working with Stack Navigation

I am new to react-native and I am trying to implement a simple application using StackNavigation and react-redux with welcome and signup screens. I have configured both the screens using StackNavigation but for some reasons , only the SignUp screen pops up when the app starts. Below are my files :

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

Below are my components 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);

I also have action and reducer files for each of my component.But they are blank as of now , since I haven't yet implemented the redux part.I am combining the reducers as below.

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

As mentioned before , even after setting the initialRouteName to Welcome in my routes.js , the SignUp screen appears first everytime I launch the app. Please help

Upvotes: 1

Views: 404

Answers (1)

Akshay Barge
Akshay Barge

Reputation: 113

I found out what was the issue. I was calling this.props.navigate inside the render function by mistake which was causing the navigation to different screen.

Upvotes: 1

Related Questions