我对React本机还很陌生,所以请帮忙。
我有一个像这样的物体
{
"id": "5c496a666e44253f0cbad0e7",
"productId": "SPMT-MACBOOK",
"owner": "5c40442b6e4425163915e5b3",
"name": "Macbook Air",
"description": "sida",
"logo": "5c40442b6e4425163915e5b3_1548315236597.jpg",
"components": [
{
"amount": 200,
"productId": "LXT",
"price": 230,
"name": "Lò xo thân",
"id": "5c9e3f0aa289e606b46a7fe0"
},
{
"amount": 2000,
"productId": "LXCT",
"price": 230,
"name": "Wing spring",
"id": "5c9e3f0aa289e606b46a7fe0"
}]
}
现在,我想在编辑时设置状态量
this.setState(prevState => ({
product: {
components: {...prevState.product.components, amount}
}
}));
但这是不对的。
此外,如果要删除产品上的组件,该如何设置状态?我声明了一个变量dataCom,并通过this.state.product.components进行设置。但这也行不通。
onRemoveComponent = (data) => {
const{product} = this.state;
const {dataCom} =this.state.product.components;
//const {dataCom} = this.state;
console.log(data);
Alert.alert(
'Remove component',
"Are you sure to detele " + data.name,
[
{text: 'Agree', onPress: () => {
this.props.onDeleteComponent(product.id, data.id);
this.setState({dataCom});
//...prevState.product.components}));
//this.setState({product});
}},
{text: 'Cancel'},
],
{ cancelable: false }
);
};
如何使用这样的嵌套对象设置状态?谢谢您的帮助