FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Use Boolean II

10mins

Create an improved version of a utility function useBoolean(initialValue) that manages a boolean state.

The function should:

  • Accept an initial boolean value.
  • Return an object with:
    • 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

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests