Reputation: 37
I've tried to create a special screen for navigation called Navigate.js . This is what I have written:
/**
* Navigate.js
*
* Root component of the app,
* responsible for setting up routes.
*
*/
// Libs
import React from 'react';
import { View, Text, Button } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
// Components
import OfferScreen from './screens/OffersScreen';
import Post from './screens/Post';
/**
* createStackNavigator
*
* Creates a stack of our routes.
*
*/
const Navigator = createStackNavigator({
OfferScreen: { screen: OfferScreen },
Post: { screen: Post },
});
/**
* createAppContainer
*
* Responsible for managing app state and linking
* the top-level navigator to the app environment.
*
*/
const Nav = createAppContainer(Navigator); export default Nav;
In OfferScreen i have this code :
static navigationOptions =({}) =>({
title: "Oferte",
headerTitleStyle: { alignSelf: 'center', },
headerStyle: {
backgroundColor: '#BA272A',
},
headerRight: (
<View style={{paddingRight:11}}>
<Button
color="#ff5c5c" title="Tombola"
onPress={() =>
this.props.navigation( 'Post' )}
/>
</View>
),
headerTintColor: 'white',
headerTitleStyle: {
fontWeight: 'bold',
},
})
The error says this : undefined is not an object ( evaluating 'OfferScreen.props.navigation' ) Please help me with this error. I'm struggling with navigation :(
Upvotes: 0
Views: 71
Reputation: 13906
You deliver the Navigation object to the navigationOptions
.
And use navigation.push
or navigation.navigate
to move the screen.
Each time you call
push
we add a new route to thenavigation
stack. When you callnavigate
it first tries to find an existing route with that name, and only pushes a new route if there isn't yet one on the stack.
static navigationOptions = ({ navigation }) => {
title: "Oferte",
headerTitleStyle: { alignSelf: 'center', },
headerStyle: {
backgroundColor: '#BA272A',
},
headerRight: (
<View style={{paddingRight:11}}>
<Button
color="#ff5c5c" title="Tombola"
onPress={() =>
navigation.push( 'Post' )}
/>
</View>
),
headerTintColor: 'white',
headerTitleStyle: {
fontWeight: 'bold',
},
})
Example
import React from 'react';
import { Button, View, Text } from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation'; // Version can be specified in package.json
class HomeScreen extends React.Component {
static navigationOptions =({navigation}) =>({
title: "Oferte",
headerTitleStyle: { alignSelf: 'center', },
headerStyle: {
backgroundColor: '#BA272A',
},
headerRight: (
<View style={{paddingRight:11}}>
<Button
color="#ff5c5c" title="Tombola"
onPress={() =>
navigation.push( 'Details' )}
/>
</View>
),
headerTintColor: 'white',
})
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => this.props.navigation.navigate('Details')}
/>
</View>
);
}
}
class DetailsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
}
const RootStack = createStackNavigator(
{
Home: HomeScreen,
Details: DetailsScreen,
},
{
initialRouteName: 'Home',
}
);
const AppContainer = createAppContainer(RootStack);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
Upvotes: 1