简体   繁体   English

从 object 中删除密钥并更新无状态组件

[英]delete key from object and updating stateless components

 const [description, setDescription] = React.useState({
   "key1":"first value",
   "key2":"second value",
   "key3":"third value"
 }
)

I have an object like the one provided above.我有一个 object,就像上面提供的一样。 i tried deleting a key from the object with the function below.我尝试使用下面的 function 从 object 中删除一个密钥。

const remove = (key) => {
   delete description[key]
}

the problem i am having is, when i call this function the key is deleted form the object, but react does not update the state.我遇到的问题是,当我调用此 function 时,密钥已从 object 中删除,但反应不会更新 state。

Note that you're mutating a local instance of your state.请注意,您正在改变 state 的本地实例。 You should update the state through the useState hook.您应该通过useState挂钩更新 state。

const remove = (key) => {
   setDescription(description => ({
     ...description,
     [key]: undefined
   }))
}

Note that you should never mutate your state.请注意,您永远不应该改变您的 state。 This can cause bugs in your application, So in the code above, we make a new object, copy the data we have, and then just set [key] to undefined, effectively deleting the value there.这可能会导致您的应用程序出现错误,因此在上面的代码中,我们创建了一个新的 object,复制我们拥有的数据,然后将[key]设置为 undefined,有效地删除那里的值。

the problem i am having is, when i call this function the key is deleted form the object, but react does not update the state.我遇到的问题是,当我调用此 function 时,密钥已从 object 中删除,但反应不会更新 state。

That is because you should not mutate state.那是因为你不应该改变 state。 Using delete description[key] will mutate the state, and doesn't inform React that a re-render should be triggered.使用delete description[key]将改变 state,并且不会通知 React 应该触发重新渲染。

You should use setDescription to inform React about a state change.您应该使用setDescription通知 React state 更改。 And instead of mutating the existing state, create a new one without the key property.而不是改变现有的 state,而是创建一个没有key属性的新的。 This can easily be done with destructuring assignment .这可以通过解构赋值轻松完成。

const remove = (key) => {
  setDescription(({ [key]: value, ...description }) => description);
};

In the solution above we extract the value of the key property and store it in the value variable.在上面的解决方案中,我们提取了key属性的值并将其存储在value变量中。 All other properties (without key ) are collected in a new object and stored in the description variable.所有其他属性(没有key )都收集在一个新的 object 中并存储在description变量中。

This process essentially creates a copy of the old description without the key property and uses it as the new state.这个过程本质上是创建一个没有key属性的旧描述的副本,并将其用作新的 state。


If you pass remove as a property to other components you could consider usinguseCallback as well.如果您将remove作为属性传递给其他组件,您也可以考虑使用useCallback This stabilises the identity of the remove function and could result in less re-renders.这稳定了remove function 的身份,并可能导致更少的重新渲染。

const remove = useCallback((key) => {
  setDescription(({ [key]: value, ...description }) => description);
}, []);

Normally you pass your dependencies in the dependency array at the end, but because the identity of setDescription is stable you can omit this dependency.通常你在最后的依赖数组中传递你的依赖,但是因为setDescription的身份是稳定的,你可以省略这个依赖。

If all the dependencies have a stable identity or the dependency array is empty, the resulting function has a stable identity as well.如果所有依赖项都具有稳定标识或依赖项数组为空,则生成的 function 也具有稳定标识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM