With the old class-based this.setState()
method, we could return null
from a function passed to setState
to tell React not to do anything with that specific setState
call:
this.setState(({ value }) => {
if (value === 0) {
return null;
} else {
return { value: value - 1 };
}
});
I'm trying to understand what's the correct way to do this with React Hooks, is the below correct?
const [x, setValue] = useState(0);
setValue(value => {
if (value === 0) {
return value;
} else {
return value - 1;
}
});
I'm trying not to trigger a re-render if I pass the original value.