deleting keys from state object in react
to remove a key from a react state object:
-
use
usestate
the hook to store state objects. - destructure the keys and remaining properties of an object.
- set the state to the remaining properties.
import {usestate} from 'react';
export default functionapp() {
const initialstate = {
id: 1,
name: 'alice',
salary: 100,
department: 'development',
};
const [employee, setemployee] = usestate(initialstate);
const removekey = () => {
setemployee(current => {
// 👇️ remove salary key from object
const {salary, ...rest} = current;
return rest;
});
};
return (
<div><buttononclick={removekey}>clickbutton><h4>{json.stringify(employee, null, 4)}h4><hr /><h2>name: {employee.name}h2><h2>department: {employee.department}h2><h2>salary: {employee.salary}h2>div>
);
}
to remove a key from the state object, we deconstruct the key and the remaining properties and update the state with the remaining properties.
alternatively, we can use the delete operator.
import {usestate} from 'react';
export default functionapp() {
const initialstate = {
id: 1,
name: 'alice',
salary: 100,
department: 'development',
};
const [employee, setemployee] = usestate(initialstate);
const removekey = () => {
setemployee(current => {
// 👇️ create copy of state object
const copy = {...current};
// 👇️ remove salary key from object
delete copy['salary'];
return copy;
});
};
return (
<div><buttononclick={removekey}>clickbutton><h4>{json.stringify(employee, null, 4)}h4><hr /><h2>name: {employee.name}h2><h2>department: {employee.department}h2><h2>salary: {employee.salary}h2>div>
);
}
if we decide to use the delete operator, make sure ...
to create a copy of the state object using the spread syntax.
we use spread syntax ...
to unpack the object's key-value pairs into a new object and create a shallow copy.
we should not mutate state objects or arrays in react.
we setstate
pass a function to , because that function is guaranteed to be called with the current (latest) state.
const removekey = () => {
setemployee(current => {
// 👇️ create copy of state object
const copy = {...current};
// 👇️ remove salary key from object
delete copy['salary'];
return copy;
});
};
a function is passed to when calculating the next state using the previous state setstate
.
otherwise, we could run into some weird race conditions if the state object we have access to doesn't represent the latest value.
for reprinting, please send an email to 1244347461@qq.com for approval. after obtaining the author's consent, kindly include the source as a link.
article url:
related articles
publish date:2025/03/09 views:170 category:react
-
要在 react 状态下替换数组中的对象:使用 map() 方法遍历数组。 在每次迭代中,检查是否满足某个条件。 替换满足条件的对象并按原样返回所有其他对象。
how to call a function only once in react
publish date:2025/03/09 views:127 category:react
-
使用 useeffect 钩子在 react 中只调用一次函数。 当 useeffect 钩子传递一个空的依赖数组时,它仅在组件挂载时运行。 当我们必须在组件安装时获取数据时,这是首选方法。
run react hooks when a component unmounts
publish date:2025/03/09 views:81 category:react
-
卸载组件时,使用 useeffect 钩子运行react钩子。 我们从 useeffect 钩子返回的函数在组件卸载时被调用,可用于清理目的。
publish date:2025/03/09 views:181 category:react
-
在 react 中使用带有索引的 map() 方法:在数组上调用 map() 方法。传递给 map() 方法的函数会被元素和索引调用。
setting onclick listener on links in react
publish date:2025/03/09 views:182 category:react
-
要在 react 中的链接上设置 onclick 监听器:在链接上设置 onclick 属性。每次单击链接时,都会调用我们传递给道具的函数。
remove an object the state array in react
publish date:2025/03/09 views:72 category:react
-
在 react 中从状态数组中移除一个对象:使用 filter() 方法遍历数组 在每次迭代中,检查是否满足条件。将状态设置为 filter 方法返回的新数组。
how to create a sleep function in react.js
publish date:2025/03/09 views:191 category:react
-
在 react 中创建 sleep 函数:定义一个以毫秒数为参数的函数。该函数应返回一个 promise,该 promise 在提供的毫秒数后得到解决。
how to handle visibility: hidden in react
publish date:2025/03/09 views:62 category:react
-
在 react 中将 css visibility 属性设置为 hidden:在指示元素是否应该可见的状态中存储一个布尔值。有条件地在元素的样式属性中设置可见性属性。例如,style={{visibility: isvisible ? “visible”
update state when props change in react
publish date:2025/03/09 views:158 category:react
-
to update state when props change in react: pass props as dependencies to the useeffect hook. every time props change, the logic in useeffect will re-run.