React技巧之移除状态对象中的键
总览
在React中,移除state对象中的键:
- 使用
useState
钩子存储state对象。 - 解构对象中需要移除的键,以及其他的属性。
- 将state设置为其他属性。
解构
import {useState} from 'react';
export default function App() {
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>
<button onClick={removeKey}>Click</button>
<h4>{JSON.stringify(employee, null, 4)}</h4>
<hr />
<h2>name: {employee.name}</h2>
<h2>department: {employee.department}</h2>
<h2>salary: {employee.salary}</h2>
</div>
);
}
为了移除state
对象中的键,我们解构了指定的键以及其余参数,并更新state
对象为其余参数。
delete
同样的,你也可以使用delete
操作符。
import {useState} from 'react';
export default function App() {
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>
<button onClick={removeKey}>Click</button>
<h4>{JSON.stringify(employee, null, 4)}</h4>
<hr />
<h2>name: {employee.name}</h2>
<h2>department: {employee.department}</h2>
<h2>salary: {employee.salary}</h2>
</div>
);
}
如果你决定使用delete
操作符,请确保使用扩展语法(…
)创建一份state
对象的副本。我们使用扩展语法来解包对象的键值对到新的对象中,并创建了浅复制。
我们永远不应该在React中改变state
对象或数组。
我们将函数传递到setState
,因为函数保证以当前(最新的)状态调用。
const removeKey = () => {
setEmployee(current => {
// ?️ create copy of state object
const copy = {...current};
// ?️ remove salary key from object
delete copy['salary'];
return copy;
});
};
当使用前一个状态计算下一个状态时,传递一个函数给setState
。否则,如果我们所访问的state
对象不代表最新的值,我们可能会得到一些奇怪的Race Condition。
总结
可以通过解构或者delete
操作符来删除state
对象中指定的键,同时需要在setState
中传入函数,保证以最新的状态调用。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。