I try to implement a navigation in React Native with react-navigation according to this tutorial, but I am facing the following error when launching the app:
undefined is not an object (Evaluating 'this.props.navigation.navigate')
render index.android.js:17:12
My index.android.js:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Button
} from 'react-native';
import {
StackNavigator,
} from 'react-navigation';
export default class Abc extends Component {
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Button
title="Go to List"
onPress={() =>
navigate('Liste')
}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
export class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Go to List"
onPress={() =>
navigate('Profile')
}
/>
);
}
}
export class Liste extends React.Component {
static navigationOptions = {
title: 'Liste',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Liste"
/>
);
}
}
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
});
AppRegistry.registerComponent('Abc', () => Abc);
Do you have any suggestions? Thank you!
I believe what you wanted to do is to register your App component in your AppRegistry instead of the Abc component.
The reason that you ran into this undefined is not an object (Evaluating 'this.props.navigation.navigate') is because the props.navigation is not available in your Abc component. If you look closely to the bottom of your code,
you have:
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
});
AppRegistry.registerComponent('Abc', () => Abc);
So you have created a StackNavigator component, but you are not registering this component but instead you are registering the Abc component in the AppRegistry, and since Abc component is not any of the screen under your StackNavigator, it won't have access to the this.props.navigation thus you would run into the exception.
You don't have Profile screen.
If you want to go to Liste screen from HomeScreen use:
onPress={() => navigate('Liste')}
If you want to go to Profile screen then you need to make Profile Screen and add it on StackNavigator, example:
export class Profile extends React.Component {
static navigationOptions = {
title: 'Profile',
};
render() {
const { navigate } = this.props.navigation;
return (
<Button
title="Profile"
/>
);
}
}
const App = StackNavigator({
Home: { screen: HomeScreen },
Liste: { screen: Liste },
Profile: {screen: Profile},
});
almost 3 years since the original question was asked. I had the same problem and landed on this page.
react-navigation is v5 now. The problem I had was because 'navigation' was not passing to the child component.
After a bit more research I fixed the problem using this solution from here.
I need to pass the props to child component like this.
<GoToButton navigation={props.navigation} />
https://reactnavigation.org/docs/connecting-navigation-prop
Hopefully this helps to new comers like me.
Related
I'm trying to set a top menu and a tab menu in my app. The bottom tabs are working, but the top menu is not displaying. The top menu that I am trying to set is the one we click and the left menu options show.
class App extends Component {
render() {
const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));
return (
<Provider store={store}>
<AppContainer
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
</Provider>
);
}
}
export default App;
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
}
}
class SettingsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
}
}
const TabNavigator = createBottomTabNavigator({
Home: HomeScreen,
Settings: SettingsScreen,
});
const MainStackNavigator = createStackNavigator({
Home: {
screen: TabNavigator
}
});
const AppDrawerNavigator = createDrawerNavigator({
Home:{
screen: MainStackNavigator
}
});
const AppSwitchNavigator = createSwitchNavigator({
Login: { screen: Login },
Main: { screen: AppDrawerNavigator }
});
const AppContainer = createAppContainer(AppSwitchNavigator);
I'm new with react native, so I am not sure the order to set the navigator options. What I'm doing wrong?
Thanks
I've put some code as an example with a MainDrawerNavigator that contains a MainTabNavigator. The tab navigator contains three stack navigators for this purpose I'll only reference the HomeScreenNavigator which is a stack navigator. By default this will show a stack header inside my Home tab but will not show a drawer icon to open the drawer. In order to do this you need to put an icon in to toggle the drawer. I've shown this by accessing the navigationOptions in the HomeScreen specifically shown:
static navigationOptions = (navData) => {
return {
headerLeft: (
<View style={styles.headerButtonLeft}>
<HeaderButtons HeaderButtonComponent={DefaultHeaderButton}>
<Item title="menu" iconName="ios-menu" onPress={() => {
navData.navigation.toggleDrawer()
}} />
</HeaderButtons>
</View>
),
}
}
The above will set an a header button on the left of the HomeScreen and pressing the Item will trigger toggling the drawer open via navData.navigation.toggleDrawer(). Your example would be similar except your outermost navigator would be your switch navigator it seems.
Full code as an example shown below: (let me know if clarification is needed elsewhere).
HomeScreen example:
class HomeScreen extends React.Component {
constructor(props) {
super(props)
this.state = { ... }
}
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
}
static navigationOptions = (navData) => {
return {
headerLeft: (
<View style={styles.headerButtonLeft}>
<HeaderButtons HeaderButtonComponent={DefaultHeaderButton}>
<Item title="menu" iconName="ios-menu" onPress={() => {
navData.navigation.toggleDrawer()
}} />
</HeaderButtons>
</View>
),
}
}
}
HomeScreenNavigator example:
import { createStackNavigator } from 'react-navigation-stack';
import HomeScreen from '../screens/HomeScreen';
import MediaSelectScreen from '../screens/MediaSelectScreen';
import FinalizePostScreen from '../screens/FinalizePostScreen';
import { userInterface } from '../constants/Colors';
import Styles from '../constants/Styles';
const HomeScreenNavigator = createStackNavigator({
Home: HomeScreen,
MediaSelect: MediaSelectScreen,
FinalizePost: FinalizePostScreen
}, {
defaultNavigationOptions: {
headerStyle: {
backgroundColor: userInterface.accentColor,
height: Styles.HEADER_HEIGHT,
},
}
});
export default HomeScreenNavigator;
MainTabNavigator example:
import { createBottomTabNavigator } from 'react-navigation-tabs';
import MessagesScreen from '../screens/MessagesScreen';
import HomeScreenNavigator from './HomeScreenNavigator';
import LearnScreenNavigator from './LearnScreenNavigator';
const MainTabNavigator = createBottomTabNavigator({
LearnScreen: { screen: LearnScreenNavigator, navigationOptions: () => ({
tabBarLabel: 'Learn'
})},
HomeScreen: { screen: HomeScreenNavigator, navigationOptions: {
tabBarLabel: 'Home'
}},
MessagesScreen: { screen: MessagesScreen, navigationOptions: {
tabBarLabel: 'Messages'
}},
});
export default MainTabNavigator;
MainDrawerNavigator example:
import { createDrawerNavigator } from 'react-navigation-drawer';
import { createAppContainer } from 'react-navigation';
import MainTabNavigator from './MainTabNavigator';
const MainDrawerNavigator = createDrawerNavigator({
DrawerNav: MainTabNavigator
}, {
drawerType: 'slide'
});
export default createAppContainer(MainDrawerNavigator);
App.js example:
import React from 'react';
import * as Font from 'expo-font';
import { AppLoading } from 'expo';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import AppSwitchNavigator from './navigation/AppSwitchNavigator';
export default class App extends React.Component {
state = { ... }
render() {
return (
<Provider store={store}>
<AppSwitchNavigator/>
</Provider>
);
}
}
const store = createStore(rootReducer);
const rootReducer = combineReducers({ ... });
Help me please. I am making mobile app. I have bottom tab navigation that is making navigation between 3 main pages. I have done navigation between 1st page and second,but not with tab bottom navigation, with button. When i have made this i have not got something i want. I will be very happy if you help me. When i navigate to second page from first i have 2 titles. But i want to make 1:
there is 2 titles, first is < and second is "Courses", i want to make it like this "< Courses". Help me please. Code:
// 1st page
import Courses from './Courses'
<Button
onPress={() => {
navigate('Courses')
}}
title="More courses"
/>
const RootStack = createStackNavigator(
{
Home: {
screen: HomeScreen,
},
Courses: {
screen: Courses,
},
Details: {
screen: DetailsScreen,
},
},
{
initialRouteName: 'Home',
}
);
const AppContainer = createAppContainer(RootStack);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
App.js
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { createBottomTabNavigator, createAppContainer } from 'react-navigation';
import Icon from 'react-native-vector-icons/Ionicons'
import Home from './screens/Home'
import Courses from './screens/Courses'
import Editor from './screens/Editor'
import AppNavigator from './AppNavigator';
const TabNavigator = createBottomTabNavigator({
Home:{
screen:Home,
navigationOptions:{
tabBarLabel:'Home',
tabBarIcon:({tintColor})=>(
<Icon name="ios-home" color={tintColor} size={24} />
)
}
},
Courses:{
screen:Courses,
navigationOptions:{
tabBarLabel:'Courses',
tabBarIcon:({tintColor})=>(
<Icon name="ios-school" color={tintColor} size={24} />
)
}
},
Editor:{
screen:Editor,
navigationOptions:{
tabBarLabel:'Editor',
tabBarIcon:({tintColor})=>(
<Icon name="ios-document" color={tintColor} size={24} />
)
}
},
},{
tabBarOptions:{
activeTintColor:'#db0202',
inactiveTintColor:'grey',
style:{
fontSize:3,
height:45,
backgroundColor:'white',
borderTopWidth:0,
elevation: 5
}
}
});
export default createAppContainer(TabNavigator);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
You don't need to define another navigator in your Home screen. If you define Home like this it should work:
import React from 'react'
import { Button, View } from 'react-native'
export default class Home extends React.Component {
render() {
const { navigation: { navigate } } = this.props
<Button
title='More Courses'
onPress={() => navigate('Courses')}
/>
}
}
Your other screens should look similar to this.
I'm new to React Native and I just don't get how react-navigation works. I've already spend multiple work days and tried different variants, but I don't even get the examples from https://reactnavigation.org/docs/intro/ to work.
My project is created with create-react-native-app AwesomeProject, like it's described on the React Native Docs.
So I went back to my last working point (without routing).
This is my File Structure:
root
- src
- - screens
- - - LoginScreen.js
- - - HomeScreen.js
- App.js
In my App.js I just perform an easy call:
import LoginScreen from './src/screens/LoginScreen';
export default LoginScreen;
This is a simplified version of my LoginScreen.js:
import React from 'react';
import {
StyleSheet,
Text,
View,
AppRegistry,
Button,
ReactNative,
} from 'react-native';
/**
* Actual View
*/
class LoginScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
// some values
}
}
render() {
return (
<View style={[styles.containerOne]}>/>
// Some Input fields
<Button
onPress={this.loginButtonOnPress.bind(this)}
title="Login"
style={[styles.loginButton]}
/>
</View>
);
}
loginButtonOnPress() {
// Validation with fetch
}
}
export default LoginScreen;
/**
* Style
*/
const styles = StyleSheet.create({
containerOne: {
marginTop: -160,
justifyContent: 'center',
flex: 1,
alignItems: 'center',
}
loginButton: {
color: 'blue',
marginTop: 10,
}
});
The LoginScreen.js should be the first displayed page. I want to load my HomeScreen.js file when loginbuttonOnPress get called. How am I supposed to do that? I've already found something like
const SimpleApp = StackNavigator({
Login: { screen: LoginScreen },
Home: { screen: HomeScreen },
});
But I don't get this to work, too.
I'm thankfull for every help I can get.
Press login button then go to HomeScreen.
render() {
return (
<View style={[styles.containerOne]}>/>
// Some Input fields
<Button
onPress={() => navigation.navigate('Home'))}
title="Login"
style={[styles.loginButton]}
/>
</View>
);
}
You can try it work
App.js
const SimpleApp = StackNavigator({
Login: { screen: LoginScreen },
Home: { screen: HomeScreen },
},{ swipeEnabled: false }
);
export default SimpleApp
Try this inside you function
loginButtonOnPress() {
this.props.navigation.navigate('Home')
}
I'm trying to build an app in react native where you hit a button and it loads a random screen. Pretty simple, but I can't get the navigator working.
import React from 'react';
import {
AppRegistry,
Text,
View,
Button,
StyleSheet,
Image,
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import Slide0 from './slides/0';
import Slide1 from './slides/1';
import Slide2 from './slides/2';
randomScreen = Math.floor(Math.random() * 2);
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Home',
};
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<Image
source={require('./assets/icons/app-icon.png')}
/>
<Text style={{ marginTop: 100 }}>Welcome to Dementia Care Activities.</Text>
<Text style={{ marginTop: 100 }}>{randomScreen}</Text>
<Button
onPress={() => navigate('Slide0')}
title="Load Random Slide"
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
const SimpleAppNavigator = StackNavigator({
Home: { screen: HomeScreen },
Slide0: { screen: Slide0 }
});
const AppNavigation = () => (
<SimpleAppNavigator />
);
export default class App extends React.Component {
render() {
return (
<AppNavigation/>
);
}
}
So I'm trying to set randomScreen to a number, and that does work and I can see it in the , but I just cant figure out how to insert that number into
const SimpleAppNavigator = StackNavigator({
Home: { screen: HomeScreen },
Slide0: { screen: SlideINSERT-RANDOM-NUMBER }
});
You could just create a list of Screen objects. It may seem redundant since you are including integers to differentiate the Screen objects, but if you ever decide to change those object names to something different, the map will already have you covered. So something like:
const screens = [Screen0, Screen1]
Then:
const SimpleAppNavigator = StackNavigator({
Home: { screen: HomeScreen },
Slide0: { screen: screens[randomScreen] }
});
Im doing this inside the react native platform using expo.
I want to display the list of items ( ListItems.js) All_Employees_screen.js . These items are being rendered via a functional component, I want to have a onRowPress() handler to so that upon clicking it i can navigate it to another view, but I dont know how to do it on react-navigation ?
Or since the new functional component can be a class component( this would be better ) how can i access the navigation thing inside it ?
AllProperties.js
import _ from 'lodash';
import React, {
Component
} from 'react';
import {
Button,
ListView,
ScrollView
} from 'react-native';
import ListItem from './ListItem';
import { connect } from 'react-redux';
import { propertiesFetch } from '../../actions';
// import { FormLabel, FormInput } from 'react-native-elements'
class AllPropertiesScreen extends React.Component {
componentWillMount(){
this.props.propertiesFetch();
this.createDataSource(this.props);
}
// we do this componentWillMount & componentWillReceiveProps (nextProps) thing twice, coz once the component is
// loaded it loads all teh values but when user hits another view like Create property, The Property data still exists
// in the global state object,
// we could move all the dc dataSource code into componentWillReceiveProps but its actually gonna benefit us
// if we make sure that we try to build our data source both when the component first loads up
// & when second time after we go back and forth other compoennts.
componentWillReceiveProps(nextProps){
// nextProps are the next set of props that this component will be rendered with
// this.props is still the old set of props
this.createDataSource(nextProps);
}
createDataSource({ properties }){
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.dataSource = ds.cloneWithRows(properties);
}
static navigationOptions = ({ navigation }) => {
const {state, setParams} = navigation;
return {
title: 'All Emplooyee',
headerRight: (
<Button
title='Add'
// onPress={() => setParams({ mode: isInfo ? 'none' : 'info'})}
onPress={() => navigation.navigate('createProperty')
}
/>
),
};
};
goBack(){
console.log('65 - go Back clicked');
}
renderRow(property){
// console.log('67-AllPropertiesScreen =', property);
return <ListItem property={property}
onPress={() => { console.log('65 - go Back clicked') }}
/>;
}
render() {
console.log('72-AllPropertiesScreen this.props', this.props );
return(
<ListView
enableEmptySections
dataSource={this.dataSource}
renderRow={this.renderRow}
/>
);
}
}
const mapStateToProps = state => {
console.log('83 - AllPropertiesScreen state. properties', state );
const properties = _.map(state.properties, (val, uid ) => {
return { ...val, uid }; // { shift: 'Monday'}
});
return { properties };
};
export default connect(mapStateToProps, {propertiesFetch}) (AllPropertiesScreen);
ListItem.js
import React, { Component } from 'react';
import { Text, TouchableWithoutFeedback, View } from 'react-native';
class ListItem extends Component {
// onRowPress(){
// Actions.employeeEdit({ employee: this.props.employee });
// }
render(){
const { agent_name, cell, address } = this.props.property;
console.log('14- ListItem ', this.props);
return (
<View>
<CardSection>
<Text style={styles.titleStyle}>
name
</Text>
<Text style={styles.titleStyle}>
cell
</Text>
<Text style={styles.titleStyle}>
address
</Text>
</CardSection>
</View>
);
}
}
const styles = {
titleStyle: {
fontSize: 18,
paddingLeft: 15
}
}
export default ListItem;
//
main.js ( this is where I have all the navigation paths hookedup.
class App extends React.Component {
render() {
const MainNavigator = TabNavigator({
// auth: { screen : AuthScreen },
// review: { screen: ReviewScreen },
// signup: { screen : SignupScreen },
followup: { screen: FollowupScreen }, welcome: { screen : WelcomeScreen },
auth: { screen : AuthScreen },
signup: { screen : SignupScreen },
main: {
screen: TabNavigator ({
followup: { screen: FollowupScreen },
map: { screen: MapScreen },
deck: { screen: DeckScreen },
settings : {
screen: StackNavigator ({
settings: { screen: SettingsScreen },
// settings: { screen: SettingsScreen },
UserProfile: { screen: UserProfileScreen },
HelpSupport: { screen: HelpSupportScreen },
Notifications: { screen: NotificationsScreen },
Signout: { screen: SignoutScreen } // not working, Navigation object not accessible inside the component
}) //screen: StackNavigator ({
},
followup : {
screen: StackNavigator ({
followup: { screen: FollowupScreen },
allProperties: { screen: AllPropertiesScreen },
createProperty: { screen: PropertyCreateScreen },
Red: { screen: RedPriorityScreen }, // not working, Navigation object not accessible inside the component
GreyPriority: { screen: GreyPriorityScreen },
}) //screen: StackNavigator ({
},
draw: {
screen: DrawerNavigator ({
drawin: { screen: DrawScreen },
}) //screen: StackNavigator ({
}
}) //screen: TabNavigator
}
}, {
navigationOptions: {
tabBarVisible: false
},
lazy: true
});
return (
<Provider store={store}>
<View style={styles.container}>
<MainNavigator />
</View>
</Provider>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
// alignItems: 'center',
justifyContent: 'center',
},
});
Expo.registerRootComponent(App);
Solution suggested by #Matt but as soon as I put the navigation={this.props.navigation} it complains. undefined is not an object ( evaluating this.props.navigation )
renderRow(property){
return (
<ListItem
property={property}
navigation={this.props.navigation}
onPress={() => {
console.log( '70-on Press inside renderRow ');
}}/>
);
}
If the component is not a screen you have to import the navigation.
Try this:
import React from 'react';
import { Button } 'react-native';
import { withNavigation } from 'react-navigation';
class MyBackButton extends React.Component {
render() {
return <Button title="Back" onPress={() => { this.props.navigation.goBack() }} />;
}
}
// withNavigation returns a component that wraps MyBackButton and passes in the
// navigation prop
export default withNavigation(MyBackButton);
For more info check out
https://reactnavigation.org/docs/connecting-navigation-prop.html
This answer was written for old version of react-navigation V1
I had the same exact problem, and I found out that this.props.navigation is injected only in components that are registered as screen in StackNavigator or TabbNavigator.
but in general you can use navigate from NavigationActions class (source here https://v1.reactnavigation.org/docs/navigation-actions.html#navigate)
note: NavigationActions.navigate receives parameters in different way but works the same way.
so this working for me
import { NavigationActions } from 'react-navigation';
let {navigate} = NavigationActions;
renderRow(property) {
return (
<ListItem
property={property}
onPress={() => { navigate({
routeName: 'OtherRoute'
});
}}/>
);
}
<MyComponent navigation={this.props.navigation}/>
Main problem is here. You didn't define your prop navigation in component. You should add this.
Here's how you can use navigation.navigate inside a functional component:
import { Text, TouchableHighlight } from 'react-native';
const MyComponent = ({ navigation }) => (
<TouchableHighlight
onPress={() => navigation.navigate('OtherRoute')}
underlayColor="blue"/>
<Text>Click to Navigate!</Text>
</TouchableHighlight>
);
export default MyComponent;
When you render MyComponent, you will need to pass navigation as a prop. For example, assume HomeContainer is a screen component:
import React from 'react';
import MyComponent from './MyComponent';
export default HomeContainer extends React.Component {
render() {
return (
<MyComponent navigation={this.props.navigation}/>
);
}
}
Change your renderRow method to the following:
renderRow(property) {
return (
<ListItem
property={property}
onPress={() => { this.props.navigation.navigate('OtherRoute'); }}/>
);
}
where 'OtherRoute' is the name of the route you want to navigate to for that row.