TyForHelpDude
TyForHelpDude

Reputation: 5002

react-native-navigation cant navigate to another page

I searched and compare it to proper solutions nothing look wrong but I almost get the error screen shown below;

enter image description here

Whats wrong with navigation code here;

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import About from './app/components/About';

export default class Home extends Component {
  static navigationOptions = {
    title: 'Welcome',
  };
  navigateToAbout(){
    const { navigate } = this.props.navigation;
    navigate('About')
  }

  render() {

    return (
      <View style={styles.container}>
        <TouchableOpacity onPress={()=>this.navigateToAbout()}>
          <Text>Go to About Page</Text>
        </TouchableOpacity>
      </View>
    );
  }
}
const SimpleApp = StackNavigator({
  Home: { screen: Home },
  About: { screen: About },
});

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

Upvotes: 1

Views: 9513

Answers (2)

Satrio Adi Prabowo
Satrio Adi Prabowo

Reputation: 600

I think your code is fine unless you have to bind the method (usually I use an arrow function, but it's ok if you want to bind the method in you constructor), maybe you can try like this way:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import About from './app/components/About';

export default class Home extends Component {
  static navigationOptions = {
    title: 'Welcome',
  }

  navigateToAbout = () => {
    this.props.navigation.navigate('About');
  }

  render() {
    const { navigate } = this.props.navigation;
    return (
      <View style={styles.container}>
        <TouchableOpacity onPress={ this._navigateToAbout }
          <Text>Go to About Page</Text>
        </TouchableOpacity>
      </View>
    );
  }
}
const SimpleApp = StackNavigator({
  Home: { screen: Home },
  About: { screen: About },
});

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

I hope it can help you :)

Upvotes: 5

mkatanski
mkatanski

Reputation: 508

You didn't connect your home component with navigation, you should pass navigation method to the mapDispatchToProps object of connect method.

Here is an example:

import { connect } from 'react-redux'
import { NavigationActions } from 'react-navigation'

const navigate = NavigationActions.navigate

export class Home extends Component {
  static navigationOptions = ({ navigation }) => ({
    title: 'Welcome',
  })
  static propTypes = {
    navigate: T.func.isRequired,
  }

  navigateToAbout(){
    const { navigate } = this.props.navigation;
    navigate({ routeName: 'About' })
  }

  render() {

    return (
      <View style={styles.container}>
        <TouchableOpacity onPress={()=>this.navigateToAbout()}>
          <Text>Go to About Page</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

export default connect(null, { navigate })(Home)

Upvotes: 1

Related Questions