Create an improved version of a utility function useBoolean(initialValue) that manages a boolean state.
The function should:
value: the current boolean state.setTrue: a function that updates the state to true.setFalse: a function that updates the state to false.toggle: a function that flips the current state.This version should avoid unnecessary redefinitions of handlers, ensuring optimal performance.
Examples
const flag = useBoolean(false); console.log(flag.value); // false flag.setTrue(); console.log(flag.value); // true flag.toggle(); console.log(flag.value); // false
JavaScript Function
No test results yet
Click "Run" to execute tests