如何使用 useState 更改子项的数据?

How to change data of children with useState?

const [blocks, setBlocks] = useState({
   "time": null,
   "blocks": [
      {
         "key": 1,
         "data": "This is some data"
      }
   ]
});

如何使用 setBlocks 将“数据”更改为“这是其他数据”?

useState的set方法不仅接受数据,还接受函数

setState(prevState => {
  return {...prevState, ...updatedValues};
});

所以你可以这样写。

setBlocks(prevState => ({
  ...prevState,
  blocks: prevState.blocks.map(v => {
    return (v.key === 1)
      ? {...v, data: 'this is some other data'}
      : v;
  }),
});