admin管理员组文章数量:1334148
I need to navigate between two view in React Native. But the problem is my ponent where the button to navigate is on an other ponent. I use the react-navigation.
Let me show you :
I have my ponent MainPage here
class MainPage extends Component {
render() {
return <View style={styles.container}>
<ComponentWithButton />
</View>
}
}
So in my ponent ComponentWithButton :
class ComponentWithButton extends Component {
goToComponent(){
this.props.navigation.push('Next')
}
render() {
return <View style={styles.container}>
<Button onPress={this.goToComponent.bind(this)}/>
</View>
}
}
My next ponent is called NextComponent
.
I have the error undefined is not an object (evaluating "this.props.navigation.push")
My stack navigator is this :
const RootStack = StackNavigator(
{
Main: {
screen: MainPage
},
Next: {
screen: NextComponent
}
},
{
initialRouteName: "Main"
},
{
navigationOptions: {
header: { visible: false }
}
}
);
I try to run my code with just one ponent it's working perfectly. I think there is problem because ComponentWithButton
is not called in my RootStack
or something like that. I don't know I am a newbie
I need to navigate between two view in React Native. But the problem is my ponent where the button to navigate is on an other ponent. I use the react-navigation.
Let me show you :
I have my ponent MainPage here
class MainPage extends Component {
render() {
return <View style={styles.container}>
<ComponentWithButton />
</View>
}
}
So in my ponent ComponentWithButton :
class ComponentWithButton extends Component {
goToComponent(){
this.props.navigation.push('Next')
}
render() {
return <View style={styles.container}>
<Button onPress={this.goToComponent.bind(this)}/>
</View>
}
}
My next ponent is called NextComponent
.
I have the error undefined is not an object (evaluating "this.props.navigation.push")
My stack navigator is this :
const RootStack = StackNavigator(
{
Main: {
screen: MainPage
},
Next: {
screen: NextComponent
}
},
{
initialRouteName: "Main"
},
{
navigationOptions: {
header: { visible: false }
}
}
);
I try to run my code with just one ponent it's working perfectly. I think there is problem because ComponentWithButton
is not called in my RootStack
or something like that. I don't know I am a newbie
2 Answers
Reset to default 4you didn't pass the navigation props to the
<ComponentWithButton />
do something like this
<ComponentWithButton navigation={this.props.navigation}/>
also the method should be
this.props.navigation.navigate('Next')
if I recall
react-navigation has a function withNavigation that populate navigation props in any of your Component. Just use it like that:
import { withNavigation } from 'react-navigation';
class ComponentWithButton extends Component {
goToComponent(){
this.props.navigation.push('Next')
}
render() {
return <View style={styles.container}>
<Button onPress={this.goToComponent.bind(this)}/>
</View>
}
}
export default withNavigation(ComponentWithButton);
本文标签: javascriptnavigation between component in react nativeStack Overflow
版权声明:本文标题:javascript - navigation between component in react native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742364752a2461063.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论