Given the state object below
{
colour: ['red', 'blue', 'green'],
size: ['small', 'medium', 'large'],
position: ['bottom', 'top', 'left', 'right'],
}
I need to be able to change/update its attribute values attrValues
as well as the attribute keys attrKey
I'm using the following logic to do so:
setAttributes((prevState) => {
const key = Object.keys(attributeToUpdate)[0];
if (key !== attrKey) {
delete prevState[key];
}
return { ...prevState, [attrKey]: attrValue };
});
If I change the attribute key for colour
to color
it works but the resulting state change to:
{
size: ['small', 'medium', 'large'],
position: ['bottom', 'top', 'left', 'right'],
color: ['red', 'blue', 'green'],
}
Moving the color
attribute to the end, I would like to keep it in its original position.
Any help or pointers will be much appreciated.
Though one should take into account the expressed concerns about key insertion order / precedence the solution the OP is looking for might come close to something like the next provided code ...