我创建了一个应用程序,用户可以在其中向car 我的示例中的特定内容添加一些注释。用户必须能够添加评论并对汽车进行评分。
const App = () => {
const [state, setState] = useState({ visible: false });
const [myData, setMyData] = useState([]);
const showModal = () => {
setState({
visible: true
});
};
const handleOk = (e) => {
setState({
visible: false
});
console.log("all data", myData);
};
const handleCancel = (e) => {
console.log(e);
setState({
visible: false
});
};
return (
<>
<Button type="primary" onClick={showModal}>
Open Modal
</Button>
<Modal
title="Basic Modal"
visible={state.visible}
onOk={handleOk}
onCancel={handleCancel}
>
<Data setMyData={setMyData} />
</Modal>
</>
);
};
在console.log("all data", myData);我必须得到结果。
现在我有这一系列的项目:
const data = [
{
cars: [
{
carName: "bmw",
carSpeed: 100
}
],
carCat: "4X4 car",
carColor: "blue"
},
{
cars: [
{
carName: "audi",
carSpeed: 150
}
],
carCat: "fast car",
carColor: "red"
},
{
cars: [
{
carName: "mercedes",
carSpeed: 170
}
],
carCat: "confort car",
carColor: "white"
}
];
当用户打开模态时,他将看到 3 个数据块,在那里他将能够添加评论和评分。
场景:如果用户在4X4 car块中添加数据(评分和评论),则以这种方式。然后保存数据,点击save按钮,数组应该以下一种方式改变:
const data = [
{
cars: [
{
carName: "bmw",
carSpeed: 100
}
],
carCat: "4X4 car",
carColor: "blue",
rate: 3, // depends by the user rate
comments: ["user's text"] // depends by user
},
{
cars: [
{
carName: "audi",
carSpeed: 150
}
],
carCat: "fast car",
carColor: "red"
},
{
cars: [
{
carName: "mercedes",
carSpeed: 170
}
],
carCat: "confort car",
carColor: "white"
}
];
如果用户不仅为一个块添加数据,而是为所有块添加数据,则数组中的所有数据都应根据以前的方式更改,具体取决于用户输入。
现在我保存时只得到一组数据。
问题:如何实现我上面描述的内容?感谢帮助。
演示:https : //codesandbox.io/s/hungry-margulis-4yx9b?file=/ Data.js: 60-489