Proposal: `useToggle` new options
See original GitHub issueClear 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that request the same feature to avoid creating a duplicate.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Perhaps the options fields should be named truthyValue and falsyValue as this aligns with the mdn glossary.
ok, thanks