所以,我想做以下事情,
const [value, setValue] = useState({})
const updateName = (name)
setValue(previousState => {
if (/*check some conditions*/) {
// dont update the state
} else {
return { /* some new state */ }
}
});
无论如何我怎样才能实现它?
你快到了。
const [value, setValue] = useState({})
setValue((prevState) => {
if (condition to not update) {
return prevState;
} else {
return newState;
}
});
您不一定需要从setValue
通话中执行此操作。你也可以这样做:
const updateSomething = () => {
if (shouldUpdate)
setValue(newState);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句