首页 > 解决方案 > 无法从不同组件的函数体内更新组件,导航问题

问题描述

我的管理员主页

导出默认函数 AdminHP(props) {

return (
    
       <View style={styles.container}>
<ImageBackground source={image} resizeMode="cover" style={styles.image}>
  <View style={styles.Button}>
        <Button title="Add / Delete products"  onPress={   props.navigation.navigate("addproduct")}/>
  </View>
     <View style={styles.Button}>
       <Button title="Check product list"   />
</View>
    </ImageBackground>
    </View>
)

}

app.js 导出默认类 App 扩展组件 {

使成为() {

return (
  <NavigationContainer>
    <Stack.Navigator initialRouteName="Login" >
      <Stack.Screen name="Login" component={Login} />
      <Stack.Screen name="Register" component={Register} />
      <Stack.Screen name="AdminHP" component={AdminHP} />
      <Stack.Screen name="UserHP" component={UserHP} />
      <Stack.Screen name="addproduct" component={addproduct} />
    </Stack.Navigator>
  </NavigationContainer>
);

} }

标签: reactjsreact-nativereact-navigation

解决方案


之前已经回答了这个问题,但是在按钮的 onPress 属性中,您应该使用像这样的箭头功能。

<Button title="Add / Delete products"  
        onPress={ () => { props.navigation.navigate("addproduct")} }
/>

推荐阅读