Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Right Button in NavigatorIOS in Tabs for React Native

I am trying to add a right button to the Navigation Bar to PUSH a View. I want to do this in a Tab Class. I am using code form the Navigation example but I can't get the Right Button to work. The tab pages are loaded fine but when I click on the Right Button I get the following message:

message: undefined is not an object (evaluating 'this.props.navigator.push')"

Main app.js

    'use strict';

    var React = require('react-native');
    var Tabs = require("./Tabs");

    var {AppRegistry} = React;

    var App = React.createClass({
      render: function () {
        return (
          <Tabs/>
        )
      }
    });

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

Here is the tabs.js

    'use strict';

    var React = require('react-native');
    var {
      NavigatorIOS,
      StyleSheet,
      TabBarIOS,
      Text,
      View
    } = React;
    var TabBarItemIOS = TabBarIOS.Item;

    var Search = require("./Search");
    var Invites = require("./Invites");
    var EmptyPage = React.createClass({

      render: function() {
        return (
          <View style={styles.emptyPage}>
            <Text style={styles.emptyPageText}>
              {this.props.text}
            </Text>
          </View>
        );
      },

    });

    var TabBarExample = React.createClass({

      statics: {
        title: '<TabBarIOS>',
        description: 'Tab-based navigation.'
      },

      getInitialState: function() {
        return {
          selectedTab: 'redTab',
          notifCount: 0,
          presses: 0,
        };
      },

      render: function() {
        return (
          <TabBarIOS
            selectedTab={this.state.selectedTab}>
            <TabBarItemIOS
              name="blueTab"
              icon={_ix_DEPRECATED('favorites')}
              accessibilityLabel="Blue Tab"
              selected={this.state.selectedTab === 'blueTab'}
              onPress={() => {
                this.setState({
                  selectedTab: 'blueTab',
                });
              }}>
              <NavigatorIOS
                style={styles.natigator}
                initialRoute={{
                  component: Search,
                  title: Search.title,
                }}
                />
            </TabBarItemIOS>
            <TabBarItemIOS
              accessibilityLabel="Red Tab"
              name="redTab"
              icon={_ix_DEPRECATED('history')}
              badgeValue={this.state.notifCount ? String(this.state.notifCount) : null}
              selected={this.state.selectedTab === 'redTab'}
              onPress={() => {
                this.setState({
                  selectedTab: 'redTab',
                  notifCount: this.state.notifCount + 1,
                });
              }}>
              <NavigatorIOS
                style={styles.natigator}
                initialRoute={{
                  component: Invites,
                  title: Invites.title,
                  rightButtonTitle: 'New Invite',
                  onRightButtonPress: () => {

                                              this.props.navigator.push({
                                               title: "test",
                                               component: EmptyPage,
                                               rightButtonTitle: 'Cancel',
                                               onRightButtonPress: () => {this.props.navigator.pop();}
                                             });}
                }}
                />
            </TabBarItemIOS>
          </TabBarIOS>
        );
      },

    });

    var styles = StyleSheet.create({

      natigator: {
        flex: 1,
      },
      tabContent: {
        flex: 1,
        alignItems: 'center',
      },
      tabText: {
        color: 'white',
        margin: 50,
      },
    });

    // This is needed because the actual image may not exist as a file and
    // is used by the native code to load a system image.
    // TODO(nicklockwood): How can this fit our require system?
    function _ix_DEPRECATED(imageUri) {
      return {
        uri: imageUri,
        isStatic: true,
      };
    }

    module.exports = TabBarExample;

Something is not right about the Navigation and I do not understand how to load a View and NavigationIOS; It seems that I can only render a class with a View or a Class with a Navigation, but not both.

Any help is appreciated.

like image 841
user1450110 Avatar asked Dec 06 '22 22:12

user1450110


1 Answers

The crash is occurring because the this object has no navigator property.

The navigator is passed as a property down to every component inside a NavigatorIOS (in the code you posted that component is Invites), if you need to access it from the current component you can use a ref to point to the NavigatorIOS that you are rendering.

The following piece of code solves this issue by creating a ref to the rendered component (ref="nav") and using it inside both callback functions.

Here you can find out more about it.

<NavigatorIOS
  ref="nav"
  style={styles.natigator}
  initialRoute={{
    component: Invites,
    title: Invites.title,
    rightButtonTitle: 'New Invite',
    onRightButtonPress: () => {
      this.refs.nav.navigator.push({
        title: "test",
        component: EmptyPage,
        rightButtonTitle: 'Cancel',
        onRightButtonPress: () => { this.refs.nav.navigator.pop(); }
      });}
  }}
/>

I do not understand the second part of the question, could you maybe point out a specific issue?

like image 184
mttcrsp Avatar answered May 25 '23 23:05

mttcrsp