无法从不同组件的函数体内更新组件,导航问题
Cannot update a component from inside the function body of a different component ,navigate problem
我的管理员主页
导出默认函数 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
export default class App extends Component {
渲染(){
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>
);
}
}
这是 answered before 但在按钮的 onPress 属性中,您应该使用这样的箭头功能。
<Button title="Add / Delete products"
onPress={ () => { props.navigation.navigate("addproduct")} }
/>
我的管理员主页
导出默认函数 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 export default class App extends Component {
渲染(){
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>
);
} }
这是 answered before 但在按钮的 onPress 属性中,您应该使用这样的箭头功能。
<Button title="Add / Delete products"
onPress={ () => { props.navigation.navigate("addproduct")} }
/>