Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

props.navigation undefined #832

Closed
vvasiloud opened this issue Mar 26, 2017 · 45 comments
Closed

props.navigation undefined #832

vvasiloud opened this issue Mar 26, 2017 · 45 comments

Comments

@vvasiloud
Copy link

Hello,
I am trying React native for the first time and I want to implement the Basic Tutorial into a new react native project but I get an "this.props.navigation" undefined error when opening the main page.

It seems that this.props doesn't have a navigation method.

In main app page:
const App = StackNavigator({ Signup: { screen: SignupScreen }, });

In page Render:
const { navigate } = this.props.navigation;
<Text onPress={() => navigate('Signup')} Sign Up</Text>

What am I may miss in this case?

@ChristianTucker
Copy link

I'm having the same problem, I went over the tutorial several times to make sure I didn't miss anything. Was wanting to give react-navigation a try since it's so popular and can't even get through the basic tutorial.

I'm on react-native 0.43 and I'm on master, so that may have to do with the problem.

@zcsongor
Copy link

zcsongor commented Apr 11, 2017

Same problem as well. Did you find any solution for this? Using the latest version: 0.43.3

@DarthRumata
Copy link

Same problem. react-navigation@1.0.0-beta.7

@DarthRumata
Copy link

Now it works. Problem was in callback for button
onPress={this.openList}>
It wasn't binded to this.

@codemonkey1337
Copy link

Same problem here (react-native 0.43.3)

@grabbou
Copy link

grabbou commented Apr 24, 2017

Can anyone please post a reproduction?

@gogeta95
Copy link

gogeta95 commented Apr 25, 2017

Had same problem, fixed it be going through this: https://reactnavigation.org/docs/intro/#Introducing-Stack-Navigator

in the last few lines:

const SimpleApp = StackNavigator({
  Home: { screen: HomeScreen }, //Default entry screen
});

AppRegistry.registerComponent('SimpleApp', () => SimpleApp);

You have to use your Navigator assigned variable i.e SimpleApp as registry component, and first screen will be the screen added on top while creating StackNavigator.

@cif
Copy link

cif commented Apr 27, 2017

I am able to reproduce this issue.

import EditProfile from './EditProfile'
class Me extends Component {
  render() {
    return (
      <View style={styles.container}>
        <MeNavbar
          gotoView={this.props.gotoView}
          onEditPress={() => console.log(this.props)}
        />
      </View>
    )
  }
}
// THE ABOVE CONSOLE OUPUTS ONLY: 
// {"navigation":{"state":{"routeName":"Base","key":"Init"}}}
// navigate method and any other expected properties of the navigation prop are undefined


const Navigator = StackNavigator(
  {
    Base: { screen: Me },
    Edit: { screen: EditProfile },
  },
  {
    // headerMode:'none',
  },
)

export default Navigator

I also tried adding AppRegistry.registerComponent('Navigator', () => Navigator) as per @gogeta95 but this did not change the result

@coreform
Copy link

coreform commented May 1, 2017

@gogeta95 's solution fixed it for me, to be clearer:

const WhateverYouWantToCallMe = StackNavigator({
  Home: { screen: HomeScreen }, //Default entry screen
});

AppRegistry.registerComponent('SimpleApp', () => WhateverYouWantToCallMe);

...and SimpleApp is (has to be) the name of your App.

@ooransoy
Copy link

ooransoy commented Jul 22, 2017

Well, I found my own fix, if you are using this.props.navigation in the constructor, just move that to the componentWillMount() function. In the constructor of a component, this.props is undefined. You can trust me, because console.debug() does not lie. Well, that is if you were using this.props.navigation in the constructor. Otherwise, I do not have a solution
.

@regitasfrk97
Copy link

@nolcay can you give me the example of your code with componentWillMount()?

@ooransoy
Copy link

ooransoy commented Aug 9, 2017

@regitasfrk97 I don't remember doing it, but I seem to have abandoned that solution and just made a function that fetches stuff from the nav, if this.props.navigation is not undefined. Otherwise, it just returns an empty function/array/string.

data = (key) => {
    return ("navigation" in this.props ? this.props.navigation.state.params.data[key] : [])
}

Or, in the render you can just return an empty View if navigation doesn't exist, because if it doesn't it is not being shown on the screen anyways.

@danijelsamec
Copy link

This worked for me.

Replace this: onPress={this.handlePress} with onPress={() => this.handlePress() }.

@MehediH
Copy link

MehediH commented Aug 24, 2017

Is there any fix for this issue yet? I am still getting the same undefined error despite doing exactly as noted by the starter docs.

@dekm
Copy link

dekm commented Aug 24, 2017

I am getting this error as well and there is no this.props.navigation when I do a log. All my other nav functions work correctly like menu and tabs. I have this setup with redux also. I think my issue might be related to using Expo as there is some extra setup you have to do for redux. Is there any work around for this?

@ianrandmckenzie
Copy link

I did a straight up copy/paste of this guide up to and including the "Adding a new screen" section here:
https://reactnavigation.org/docs/intro/

I am getting this error and am completely stumped. I made my app with create-react-native-app

@oseifrimpong
Copy link

Hey, guys, have anyone found the solution for this issue. I made my app with create-react-native-app. I have been on this error for 4 days.

@gowthamkumar7
Copy link

gowthamkumar7 commented Sep 25, 2017

It seems not working. The issue is still open. No one is assigned on this issue for the past six months.

@kelset kelset self-assigned this Sep 25, 2017
@kelset
Copy link

kelset commented Sep 25, 2017

I'll have a look at it this weekend, sorry guys but we have a huge workload and we are starting now to get back on track.

@frantisek-dobrota
Copy link

Same problem here, cannot even get through the tutorial 👎

kelset added a commit that referenced this issue Sep 30, 2017
Fixes #832 - the code in this page was wrong, that export default was causing errors.

I've also modified a few phrases here and there to help the comprehension.
@kelset
Copy link

kelset commented Sep 30, 2017

Hey guys, you were right there was a mistake in the code in that page - I've submitted a PR and I'm quite sure it will be merged soon 😉

In the meantime, basically you need to change export default const SimpleApp = StackNavigator({ to export const SimpleApp = StackNavigator({ if you go via the standard react-native route.

@GuillotJessica
Copy link

@kelset still navigate of undefined

@kelset
Copy link

kelset commented Oct 3, 2017

@GuillotJessica weird.

Can you tell me if you followed the react-native init or the create-react-native-app route?

Can you copy/paste your code so I can have a look at it?

@GuillotJessica
Copy link

GuillotJessica commented Oct 3, 2017 via email

@kelset
Copy link

kelset commented Oct 3, 2017

Well, this code seems not the one from the tutorial 😶

Anyway, your issue I think it's related to the fact that in Footer.js you are trying to use the navigation props but the component is not "attached" to the StackNavigator, only Login and BonsPlansScreen are.

--
EDIT: fix is live! https://reactnavigation.org/docs/intro/

GantMan pushed a commit that referenced this issue Oct 3, 2017
* Fixing Intro Guide

Fixes #832 - the code in this page was wrong, that export default was causing errors.

I've also modified a few phrases here and there to help the comprehension.

* more fixes, for CRNA
@GuillotJessica
Copy link

GuillotJessica commented Oct 3, 2017 via email

@jackson-sandland
Copy link

jackson-sandland commented Oct 8, 2017

The issue here, as mentioned above, is that the context of the function calling this.props is not bound to the component. To resolve, you can try the following:

class BindMe extends Component {
  constructor(props){
    super(props)
    this.bindThisFunction = this.bindThisFunction.bind(this)
  }
}

This should now show defined for the navigation props!

@Ishjot
Copy link

Ishjot commented Oct 20, 2017

I have tried to bind the function calling this.props and am still running into a similar issue.

   constructor(props) {
        super(props)

        this._navigateTo = this._navigateTo.bind(this);
    }

    _navigateTo(pageName) {
        this.props.navigation.navigate(pageName)
    }

And I am calling the function later like this...
onPress={() => this._navigateTo('PageToNavigateTo')}

And I am seeing
undefined is not an object (evaluating this.props.navigation.navigate)

@rscharfer
Copy link

@Ishjot Did you figure out how to solve the problem? I have the exact same issue.

@Ishjot
Copy link

Ishjot commented Oct 29, 2017

@rscharfer yes, you need to define a StackNavigator that contains the two pages that you are trying to navigate between.

@NasarudinAbdulShukor
Copy link

@Ishjot Can you elaborate on how did you define the StackNavigator? is it on main.js / App.js where people usually define the navigator(stack, drawer, tab) for their pages or inside the function onPress?

@Ishjot
Copy link

Ishjot commented Oct 31, 2017

@connorwaslo
Copy link

connorwaslo commented Nov 4, 2017

@Ishjot I still get "TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate')" as an error even following the tutorial you just linked.

@randyqiu
Copy link

You should pass the navigation prop to the screen component of navigator YOURSELF. For example:

import PlayerView from 'player-view';
const PlayerScreen = ({ navigation }) => (<PlayerView navigation={navigation}/>);

Then you will have this.props.navigation in your component.

@cglacet
Copy link

cglacet commented Nov 20, 2017

flow-bin 0.56.0
react 16.0.0
react-navigation 1.0.0-beta.19

If you manually add a type for the Props objects that your Component is supposed to handle then it will remove the error (at least it does for me)

type Props = { navigation: Function }
class HomeScreen extends Component<Props> {
    render() {
      const { navigate } = this.props.navigation; 
      ...
   }
}

This only help ignoring the problem until a real solution is found, but it's still decent if the only thing you are doing with props is calling the navigation function.

Nevertheless I guess there is a way to retrieve the actual type of Props that should be used here, but I can't find it and it seems that most people really don't care and just ignore this error. This doesn't change much in the end because types are really just here to help you 😛.

PS, here the definition I have for React.Component:

declare class React$Component<Props, State = void>

@hugoh59
Copy link

hugoh59 commented Dec 20, 2017

I'm still experiencing the same issue, is this thing still not fixed? 🤒

@DiegoFelipe
Copy link

I'm facing the same problem I still can't understand how to do it, I got a component with a button that needs to call another screen, I'm importing StackNavigator from react-navigation and when I try to call another screen with 'this.props.navigation.navigate('Quadros')' it says that it's not an object. I could understand, by reading this issue that my component can't see my navigation props, but I haven't figured out how to pass the navigation props in order to be able to call the screen from my custom component. It's actually a screen that render a component inside drawer and this component inside drawer call another component <- this is the one that needs to call another screen.

@GuillotJessica
Copy link

show us us your code with the navigator settings

@DiegoFelipe
Copy link

I could made it work, like I said I had a 'home screen' that called a 'sidebar' custom component that called a nother custom component called webservice that called another component WSBody, to make it work I did the following:

In Home screen:

I called the sidebar component this way:

<Sidebar nav = {this.props.navigation}/>

In my Sidebar component:

constructor(props) {
    super(props);
    this.state = {};
    const nav = this.props.nav; 
  }

still in sidebar I called the webservice component this way:

<WebService nav = {this.props.nav}/>

I did the same thing that I've done in sidebar in webservice, finally in WSbody I could use:

this.props.nav.navigate('MyScreen');

I hope it help other peoples with the same issue.

@GuillotJessica
Copy link

I was thinking of something like this

const ModalStack = StackNavigator({
  Home: {
    screen: MyHomeScreen,
  },
  Profile: {
    path: 'people/:name',
    screen: MyProfileScreen,
  },
});

from documentation website
In this particular architecture, you may use the navigation props and go from Home and Profile and vice versa

1 similar comment
@GuillotJessica
Copy link

I was thinking of something like this

const ModalStack = StackNavigator({
  Home: {
    screen: MyHomeScreen,
  },
  Profile: {
    path: 'people/:name',
    screen: MyProfileScreen,
  },
});

from documentation website
In this particular architecture, you may use the navigation props and go from Home and Profile and vice versa

@GuillotJessica
Copy link

but if your are using different kind of navigation like tab then you may nest them together

@EricWiener
Copy link

You need to pass the navigation prop down to the component (it doesn't just magically get it). So if you are trying to navigate from a child component, you would need to do something like:

//parent Component
export default class Parent extends Component{
  //...
  render(){
    const {navigate} = this.props.navigation; //recieves prop from StackNavigator
    return(
      //...
      <Child navigate={navigation}>
      //...
    );
  }
}

//child Component
export default class Child extends Component{
  //...
  render(){
    const {navigate} = this.props.navigation; //this is the navigation prop passed from the prop
    return(
      //...
      <Button onPress={()=>navigate('Home')}> 
      //...
    );
  }
}

@uanthwal
Copy link

uanthwal commented Mar 8, 2018

@EricWiener : I tried your solution but no luck. May be I am missing something. Please help me out.
Here's my code:
LoginScreen.js

export default class LoginScreen extends Component {
	constructor(props) {
		super(props);
	}
	render() {
		return (
			<SignupSection navigate={this.props.navigation} />
		);
	}
}

SignupSection.js

export default class SignupSection extends Component {
	constructor(props) {
		super(props);
	}
	render() {
		const {navigate} = this.props.navigation; -- tried printing this it shows the data but on button click (added below: Create account), it says cannot find variable navigate
		return(
			<TouchableOpacity style={styles.button}
			onPress={this.props.navigation.navigate('signupScreen')} >
				{
					<Text style={styles.text}>Create account</Text>
				}
			</TouchableOpacity>
		);
	}
}

AppNavigation.js

// login stack
const LoginStack = StackNavigator({
	loginScreen: { screen: LoginScreen },
	signupScreen: { screen: SignupScreen }
}, {
	headerMode: 'float',
	navigationOptions: {
	headerStyle: {backgroundColor: '#E73536'},
	headerTintColor: 'white'
}
})

const PrimaryNav = StackNavigator({
	loginStack: { screen: LoginStack }
	}, {
	// Default config for all screens
	headerMode: 'none',
	title: 'Main',
	initialRouteName: 'loginStack',
	transitionConfig: noTransitionConfig
})

export default PrimaryNav

One thing I have noticed is if I move the sign up screen content to Loginscreen then the navigation is working but want to know the solution where we have nested navigations.

@brentvatne
Copy link
Member

hi there. please create a new issue and follow the issue template if this problem persists for you. thank you!

@react-navigation react-navigation locked and limited conversation to collaborators Mar 9, 2018
sourcecode911 pushed a commit to sourcecode911/react-navigation that referenced this issue Mar 9, 2020
* Fixing Intro Guide

Fixes react-navigation#832 - the code in this page was wrong, that export default was causing errors.

I've also modified a few phrases here and there to help the comprehension.

* more fixes, for CRNA
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests