给定下面的状态对象
{
colour: ['red', 'blue', 'green'],
size: ['small', 'medium', 'large'],
position: ['bottom', 'top', 'left', 'right'],
}
我需要能够更改/更新其属性值attrValues
以及属性键attrKey
我正在使用以下逻辑来做到这一点:
setAttributes((prevState) => {
const key = Object.keys(attributeToUpdate)[0];
if (key !== attrKey) {
delete prevState[key];
}
return { ...prevState, [attrKey]: attrValue };
});
如果我将属性键更改为colour
它color
可以工作,但结果状态更改为:
{
size: ['small', 'medium', 'large'],
position: ['bottom', 'top', 'left', 'right'],
color: ['red', 'blue', 'green'],
}
将color
属性移动到最后,我想将其保持在原来的位置。
任何帮助或指示将不胜感激。