question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: `useToggle` new options

See original GitHub issue

Clear and concise description of the problem

Sometimes we need to toggle 0 or 1. useToggle does not do this very well.

Suggested solution

export interface UseToggleOptions<Truly, Falsely> {
  trulyValue?: MaybeRef<Truly>
  falselyValue?: MaybeRef<Falsely>
}

export function useToggle<Truly, Falsely, T = Truly | Falsely>(initialValue: Ref<T>, options?: UseToggleOptions<Truly, Falsely>): (value?: T) => T
export function useToggle<Truly = true, Falsely = false, T = Truly | Falsely>(initialValue?: T, options?: UseToggleOptions<Truly, Falsely>): [Ref<T>, (value?: T) => T]

/**
 * A boolean ref with a toggler
 *
 * @see https://vueuse.org/useToggle
 * @param [initialValue=false]
 */
export function useToggle(initialValue: MaybeRef<unknown> = false, options: UseToggleOptions<true, false> = {}) {
  const {
    trulyValue = true,
    falselyValue = false,
  } = options

  const valueIsRef = isRef(initialValue)
  const innerValue = ref(initialValue) as Ref<boolean>

  function toggle(value?: boolean) {
    // has arguments
    if (arguments.length) {
      innerValue.value = value!
      return innerValue.value
    }
    else {
      innerValue.value = innerValue.value === unref(trulyValue) ? unref(falselyValue) : unref(trulyValue)
      return innerValue.value
    }
  }

  if (valueIsRef)
    return toggle
  else
    return [innerValue, toggle]
}

Alternative

No response

Additional context

No response

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
zojizecommented, Apr 25, 2022

Perhaps the options fields should be named truthyValue and falsyValue as this aligns with the mdn glossary.

1reaction
okxiaoliang4commented, Apr 25, 2022

Perhaps the options fields should be named truthyValue and falsyValue as this aligns with the mdn glossary.

ok, thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-hooks/useToggle.test.ts at master - GitHub
Essential set of React Hooks for convenient Web API consumption and state management. - react-hooks/useToggle.test.ts at master · kripod/react-hooks.
Read more >
useToggle React Hook - useHooks
Hooks are a feature in React that allow you use state and other React features without writing classes. This website provides easy to...
Read more >
Question regarding useToggle(custom hook) vs useState ...
The main purpose of introducing your own/custom hook is to tackle a scenario where none of the built-in hooks serve you properly.
Read more >
How To Use Toggle Widget In Elementor WordPress Plugin ...
... DIV FAQ Schema: Use toggle to enable or disable options for using ... https://crmrkt.com/NloAlj ⭐ Buy Website Hosting Plan and Gain a ......
Read more >
How To Use Toggl Plan 2022 - Beginners Guide TUtorial
In this video I will show you How To Use Toggl Plan 2021Iam signed with affiliate programs and companies shown in the video...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found