Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

useToggle Logic

Hooks Logiceasy

Course · Section 10: A Closer Look at Functions · Lecture 144: Closures

Implement `useToggle(initial)` returning `{ toggle(), value() }` where toggle flips a boolean and value reads it.

Examples

Input: useToggle(false): toggle, toggle, toggle, value
Output: true
Three toggles from false end at true.

Sample tests

Input: toggle three times
Output: true
Input: toggle once from true
Output: false

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Recreating state inside toggle resets it each call.

Learning resources

  • React: Custom Hooks
Approach & explanation (try first)

A closure variable flipped by toggle implements the classic useToggle hook logic.

Loading...
⌘/Ctrl + Enter

Run your code to see results.