I've a question about a behaviour that appears strange to me.
I am writing a custom hook.
const useToggle =
(initialState) => {
const [isTrue, setState] = useState(initialState || false);
const False =
() => {
setState(false);
};
const True =
() => {
setState(true);
};
return [isTrue, True, False];
};
It works fine.
But if I try to rewrite False, and True using setState.bind I get the following warning in console.
const False = setState.bind(null, false);
const True = setState.bind(null, true);
The warning:
Warning: State updates from the useState() and useReducer() Hooks don't support the second callback argument.
To execute a side effect after rendering, declare it in the component body with useEffect().
Is this the expected behaviour?
Why is that? The two snippet appear equivalent to me.
I've a question about a behaviour that appears strange to me.
I am writing a custom hook.
It works fine.
But if I try to rewrite
False, andTrueusingsetState.bindI get the following warning in console.The warning:
Is this the expected behaviour?
Why is that? The two snippet appear equivalent to me.