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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With