我有一个标题和一个购物车按钮。此按钮用于导航到购物车屏幕。当我按下按钮时,我看到错误。
错误:未定义不是对象(评估"_this3.props.navigation.navigate")
标头位于标头文件的标头类中:我在钱包屏幕等其他屏幕中使用标题。
<Header style={styles.headerStyle}>
<Body>
<Text>Logo</Text>
</Body>
<Right>
<TouchableOpacity activeOpacity={0.5} onPress={() => {
this.props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Cart'
})
],
}))
}}>
<Icon type="MaterialCommunityIcons" name="washing-machine"/>
{this.state.cartCountNumber != 0 ?(
<View style={styles.cartCountBox}>
<Text>{this.state.cartCountNumber}</Text>
</View>
):null}
</TouchableOpacity>
</Right>
</Header>
钱包屏幕:当我在钱包的内容中使用按压时工作!
export default class WalletScreen extends React.Component{
render() {
return (
<Container>
<HeaderShow /> // My header is here !!!!!!!!!!!!!!!!!!
<Content>
<Text>Wallet Screen</Text>
// When I use button and on-press here, working fine!
</Content>
</Container>
);
}
}
如果您在钱包屏幕中访问 this.props.navigation,当您将导航道具添加到钱包屏幕的标题时,它将起作用;
export default class WalletScreen extends React.Component {
render() {
return (
<Container>
<HeaderShow navigation={this.props.navigation}/> // My header is here !!!!!!!!!!!!!!!!!!
<Content>
<Text>Wallet Screen</Text>
// When I use button and on-press here, working fine!
</Content>
</Container>
);
}
}