Reputation:
I'm creating a "Menu" component that I call on every pages and it shows correctly but I can't get it working.
I can't navigate as easily as I did in my Login page: Button with OnPress Event and a Call to one function with /if correct logins/ execute: this.props.navigation.navigate("Accueil")
So... I taught that it could be the same and on my component "Menu" I tried to call the same code:
<TouchableOpacity style={[styles.menu_item_selected,styles.menu_item]} onPress={() => nav("Home")}>
<FontAwesome name="home" size={40} color="#fff"/>
<Text style={styles.menu_text}>Home</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.menu_item} onPress={() => nav("Datas")}>
<FontAwesome name="user" size={40} color="#fff"/>
<Text style={styles.menu_text}>Datas</Text>
</TouchableOpacity>
But it doesn't work, it crashes on button press.
Here is my Navigation file:
import { createAppContainer } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
import Login from '../Components/Login'
import Menu from '../Components/Menu'
import Home from '../Components/Home'
import Datas from '../Components/Datas'
const SearchStackNavigator = createStackNavigator({
Login: {
screen: Login
},
Home:{
screen: Home
},
Datas:{
screen: Datas
}
})
export default createAppContainer(SearchStackNavigator)
I tried to use props, but didn't worked too.
More info:
The usage of my "Menu" component is like class Menu extand React.Component /some code/ and export default Menu then <Menu/>
on my pages.
My tries with props looked like _nav(page){
this.props.navigation.navigate(page)
}
on pages like "Home" and "Datas" at the top before the "render(){return(" part. Then const { nav } = this.props
in my "Menu" component and something like onPress={() => nav("Donnees")}
on my menu buttons.
Any ideas? Or sample codes? I couldn't find any custom menu sample that fill my needs
Upvotes: 0
Views: 953
Reputation: 1042
You need to pass the navigation prop to your Menu component like this(Assuming you are using it in a Screen declared in your Navigation config):
<Menu navigation={this.props.navigation} />
with that your Menu component will have access to navigation prop
this.props.navigation.navigate('Datas');
Upvotes: 0