useState 更新嵌套在对象中的对象数组
useState to update array of objects nested inside object
我的状态设置如下:
const [groupState, setGroupState] = useState({
groupId: uuidv4(),
content: [],
});
我想将对象推送到 'content' 数组,但遇到困难。我查看了这个答案: 但似乎无法将其应用于问题。
目前我有:
setGroupState({
...groupState,
...content.push({test: 'test'})
});
.. 但它不起作用,我无处可去。
谁能告诉我如何排序?
push
改变数组,你应该像这样创建一个新数组:
setGroupState({
...groupState,
content: [...groupState.content, {test: 'test'}],
});
我的状态设置如下:
const [groupState, setGroupState] = useState({
groupId: uuidv4(),
content: [],
});
我想将对象推送到 'content' 数组,但遇到困难。我查看了这个答案:
目前我有:
setGroupState({
...groupState,
...content.push({test: 'test'})
});
.. 但它不起作用,我无处可去。
谁能告诉我如何排序?
push
改变数组,你应该像这样创建一个新数组:
setGroupState({
...groupState,
content: [...groupState.content, {test: 'test'}],
});