我想传递道具
this.props.navigation.navigate('LIST_PRODUCT', { product: item.product, stock: item.stock});
但结果将是
{
...this.props,
navigation: {
...,
state: {
...,
params: {
product: 'Lenovo K5 Pro',
stock: 5
}
}
}
}
结果就是我想要的
{
...this.props,
product: 'Lenovo K5 Pro',
stock: 5
}
有什么方法可以将参数从状态传递给子组件中的this.props?
答案 0 :(得分:0)
尝试以下解决方案
HomeScreen.js
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => {
/* 1. Navigate to the Details route with params */
this.props.navigation.navigate('Details', {
itemId: 86,
otherParam: 'anything you want here',
});
}}
/>
</View>
);
}
}
DetailsScreen.js
class DetailsScreen extends React.Component {
render() {
/* 2. Get the param, provide a fallback value if not available */
const { navigation } = this.props;
const itemId = navigation.getParam('itemId', 'NO-ID');
const otherParam = navigation.getParam('otherParam', 'some default value');
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Text>itemId: {JSON.stringify(itemId)}</Text>
<Text>otherParam: {JSON.stringify(otherParam)}</Text>
<Button
title="Go to Details... again"
onPress={() =>
this.props.navigation.push('Details', {
itemId: Math.floor(Math.random() * 100),
})
}
/>
<Button
title="Go to Home"
onPress={() => this.props.navigation.navigate('Home')}
/>
<Button
title="Go back"
onPress={() => this.props.navigation.goBack()}
/>
</View>
);
}
}
答案 1 :(得分:0)
我认为您可以像这样传播状态对象:
const { navigation, ...rest } = this.props;
{
...rest,
...navigation.state.params
}
请注意,state.params
可能并不总是在那里