1

I've looked at many different Stack Overflow posts about how to navigate screens in React Native but none of them seem to be working, perhaps because those posts were for older versions of React Native. Here is my code with the irrelevant parts taken out below. I'm trying to navigate from this screen to another screen called SubTasks. I made sure to say export default class SubTasks extends React.Component in SubTasks.js, by the way. The error I'm getting when I click on the button is "undefined is not an object (evaluating this.props.navigator.push'). Does anyone know where my error may be?

    import React, { Component, PropTypes } from 'react';
    import { StyleSheet,NavigatorIOS, Text, TextInput, View, Button} 
             from 'react-native';
    import SubTasks from './SubTasks';

    export default class App extends React.Component {
      constructor(props) {
        super(props);
      }                                                        
      renderScene(route, navigator) {
         if(route.name == 'SubTasks') {
           return <SubTasks navigator = {navigator} />
         }
      }

      _navigate() {
        this.props.navigator.push({
          title: 'SubTasks',
        })
      }

      render() {
        return (
          <View>
            <NavigatorIOS
              initialRoute= {{ 
                title: 'SubTasks',
                component: SubTasks,
              }} 
              style = {{ flex: 1 }}                                          
            />                                 
            <Button 
              title= 'SubTasks'
              style = {{flexDirection: 'row', fontSize: 20, alignItems: 'flex-end'}}
              color = 'blue'
              onPress= {() => this._navigate()}>
              styleDisabled = {{color: 'red'}}
            </Button>
          </View>                                                              
      )}                                                                       
   }

1 Answer 1

1

Make sure you bind your _navigate function in your constructor:

constructor(props) {
  super(props);
  this._navigate = this._navigate.bind(this);
} 

Or consider using arrow function

_navigate = () => {
  this.props.navigator.push({
    title: 'SubTasks',
  })
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! Unfortunately, after fixing that clicking the button that is supposed to take me to the SubTasks screen doesn't do anything. Kinda flustered because I can't figure out what's wrong.
@SukhpreetPabla Check out this link: facebook.github.io/react-native/docs/…, right now you are pushing a js object with only title key value pair, you will need to add the component key with SubTasks.
@SukhpreetPabla Can you accept and upvote the answer?
I already did, but I don't have 15 reputation yet (just made my acct recently) so my vote doesn't "change the publicly displayed post score." Sorry.
Also, correct me if I'm wrong but I'm assuming (since I haven't done the backend yet) that if I used the first button to navigate to Subtasks, wrote/saved some data, and then went back and navigated with the second button, that data from before would still be there? If so, Is there a way for each button to navigate to a different "version" of the same screen? For example, could each button go to its own version of Subtasks.js, or would I have to make Subtasks1.js, Subtasks2.js, Subtasks3.js, etc.? If the former isn't possible, I'd need a fixed number of nav buttons,plus a lot of memory wasted.

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.