您的代码几乎没有变化:
在您的 cangeField 函数中,您需要 3 个参数来更改要更改的字段、该字段的国家 ID 所有者以及该字段的新值。
要更改您需要的值,首先在国家列表中找到国家,然后更新该字段的值。
const changeField = (field, id, value) => {
console.log(id, country);
const newCountryInfo = [...country];
newCountryInfo.forEach(c => {
if (c._id === id) {
c[field] = value;
setCountry([...newCountryInfo]);
return;
}
});
};
在您的 textInput 中,您调用更改字段并按预期传递三个参数,首先是您要在这种情况下更改的国家/地区属性的名称“名称”-> country.name,其次是国家 ID country._id 和最后是该字段的新值
<TextField
...
onChange={event => changeField('name', item._id, event.target.value)}
...
/>
你可以注意到我删除了 ES6 函数 '=>' 之后的大括号,因为在你使用这个大括号时你需要在里面返回
<TextField
...
onChange={event => {
return changeField('name', item._id, event.target.value);
}}
...
/>
在您的代码中,您忘记了返回,但直接返回更简洁
<TextField
...
onChange={event => changeField('name', item._id, event.target.value)}
...
/>