[英]How do I control each property respectively in React(Next.js)?
import React from 'react';
import { makeStyles} from '@material-ui/core/styles';
import {Select, MenuItem} from '@material-ui/core';
import useState from 'react';
const test = () => {
const data = [
{TITLE : "Festival", PRIORITY : 3, STEP : 1},
{TITLE : "Headphone", PRIORITY : 2, STEP : 2},
{TITLE : "Mountain", PRIORITY : 1, STEP : 1}
]
return (
<>
{
data.map((info) => (
<div>
<div>{info.TITLE}</div>
<Select value={info.PRIORITY}>
<MenuItem value={1}> 1 </MenuItem>
<MenuItem value={2}> 2 </MenuItem>
<MenuItem value={3}> 3 </MenuItem>
</Select>
<Select value={info.STEP}>
<MenuItem value={1}> 1 </MenuItem>
<MenuItem value={2}> 2 </MenuItem>
<MenuItem value={3}> 3 </MenuItem>
</Select>
</div>
))
}
</>
)}
export default test;
In this code, I'm trying to control PRIORITY value and STEP value respectively.在这段代码中,我试图分别控制 PRIORITY 值和 STEP 值。
I'm having trouble because, In my Data array, I have three items.我遇到了麻烦,因为在我的数据数组中,我有三个项目。 Therefore, If I add
因此,如果我添加
const [priority, setPriority] = useState(undefined);
const [step, setStep] = useState(undefined);
const priorityChange = (e) => {
setPriority(e.target.value)
};
const stepChange = (e) => {
setStep(e.target.value)
};
and put this value in并将这个值放入
<Select value={priority} onChange={priorityChange}></Select>
...
<Select value={step} onChange={stepChange}></Select>
...
this item,这个项目,
Every item gets the same value, therefore I'm unable to control each PRIORITY and STEP value.每个项目都有相同的值,因此我无法控制每个 PRIORITY 和 STEP 值。
How can I control each item?如何控制每个项目? I need some help.
我需要一些帮助。
I might misspell.我可能拼错了。 Please be understandable!
请体谅!
Firstly your data
array is not hooked to any state managing variable.首先,您的
data
数组未连接到任何 state 管理变量。 So when you try showing values initially from the data array and then try showing the updated data from the hooks variable when an action is trigged, it is obvious to cause some clash and hence fail to show the updated values.因此,当您尝试最初显示数据数组中的值,然后尝试在触发操作时显示来自 hooks 变量的更新数据时,很明显会导致一些冲突,因此无法显示更新的值。 One way to get around this would be, to associate the initial data with a state hook and then update the
data
array accordingly.解决此问题的一种方法是将初始数据与 state 挂钩相关联,然后相应地更新
data
数组。 It is also important that correct data is updated that corresponds to the action triggered, so here we'd want to make sure that each object of the collection is unique, this could be accomplished by assigning an id
attribute on each object.更新与触发的操作相对应的正确数据也很重要,因此在这里我们要确保集合的每个 object 都是唯一的,这可以通过在每个 object 上分配一个
id
属性来完成。 Further up, we can find out the object on which the action was taken on, mutate the property value and then re-construct the array using the state hook function to re-render with the correct updated value(s).再往上,我们可以找到对其执行操作的 object,改变属性值,然后使用 state 钩子 function 重新构造数组以重新生成正确的值。 Kindly refer to the below code and read the comments to get a clearer idea.
请参考下面的代码并阅读评论以获得更清晰的想法。
import React, { useState } from "react";
const App = () => {
const [data, setData] = useState([
{ id: Math.random(), TITLE: "Festival", PRIORITY: 3, STEP: 1 },
{ id: Math.random(), TITLE: "Headphone", PRIORITY: 2, STEP: 2 },
{ id: Math.random(), TITLE: "Mountain", PRIORITY: 1, STEP: 1 }
]); //id attribute is added to each object to make sure every object in the array is unique.
const priorityChange = (e, id) => {
//This function should accept event and id arguments, to identify and update
//values correctly.
const index = data.findIndex((item) => item.id === id); //find the index of the object (item) whose priority needs to be updated.
const arr = [...data]; //Copy original array data to constant arr.
arr[index].PRIORITY = e.target.value; //mutate the PRIORITY property's value
setData([...arr]); //Set data to the new array with updated value.
};
const valueChange = (e,id) => {
//This function should accept event and id arguments, to identify and update
//values correctly.
const index = data.findIndex((item) => item.id === id); //find the index of the object (item) whose priority needs to be updated.
const arr = [...data];
arr[index].STEP = e.target.value; //mutate the STEP property's value
setData([...arr]); //Set data to the new array with updated value.
};
return (
<>
{data.map((info) => (
<div key={Math.random()}>
<div>{info.TITLE}</div>
<select
value={info.PRIORITY}
onChange={(e) => {
priorityChange(e, info.id); //pass event object and id corresponding to each array object.
}}
>
<option value={1}> 1 </option>
<option value={2}> 2 </option>
<option value={3}> 3 </option>
</select>
<select
value={info.STEP}
onChange={(e) => {
valueChange(e, info.id); //pass event object and id corresponding to each array object.
}}
>
<option value={1}> 1 </option>
<option value={2}> 2 </option>
<option value={3}> 3 </option>
</select>
</div>
))}
</>
);
};
export default App;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.