1

i am having this simple code in the same index.android.js file:

import React, { Component } from 'react';
import {
  AppRegistry,
  Text,
  View,
  Button
} from 'react-native';
import {StackNavigator} from 'react-navigation';

// First Page
class FirstPage extends Component {
  static navigationOptions = {
    title: 'Welcome',
  };

  render() {
    const { navigate } = this.props.navigation;
    return (
      <View>
        <Text>Hello First Page</Text>
        <Button 
          onPress={() => navigate('SecondPage')}
          title="Go to second page"
        />
      </View>
    );
  }
}

// Second Page
class SecondPage extends Component {
  static navigationOptions = {
    title: 'Second Page',
  };

  render() {
    const { navigate } = this.props.navigation;
    return (
      <View>
        <Text>Hello Second Page</Text>
        <Button 
          onPress={() => navigate('FirstPage')}
          title="Go to first page"
        />
      </View>
    );
  }
}


const SimpleApp = StackNavigator({
  FirstPage: { screen: FirstPage},
  SecondPage: { screen: SecondPage},
});

AppRegistry.registerComponent('MoviesTickets_YCH', () => FirstPage);

All i want is to navigate between the screens, but i have the error message: can't find variable: navigate, i have no clue why it happens, any idea how?

Thanks for your help

1

3 Answers 3

5

In the render method add the following:

const {navigate} =this.props.navigation;

Your render should look like this

render(){
const {navigate} =this.props.navigation;
return (
  .... ) ;
}

in each component you use navigate. for more infos, follow the documentation, it's pretty easy. reactnavigation

Sign up to request clarification or add additional context in comments.

4 Comments

ahhh i went through the doc but miss this : const { navigate } = this.props.navigation; bad for me, thanks
i add it just below the render as described, now i am having another error message: undefined is not an object ( evaluating 'this.props.navigation.navigate')
Change this: AppRegistry.registerComponent('MoviesTickets_YCH', () => FirstPage); to this AppRegistry.registerComponent('MoviesTickets_YCH', () => SimpleApp);
Thanks very much, that' s it, i have my head away this time, cheers
2

If i'm clear, react native docs say navigate it's an action creator and you can get acces to it in this.props.navigation;

const { navigate } = this.props.navigation; in render.

Comments

0
remove node_modules and package-lock.json
npm install
npm install --save react-navigation
npm install --save react-native-gesture-handler
react-native link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.