react-use
react-use copied to clipboard
New hook: useEnable?
Is your feature request related to a problem? Please describe.
I almost never use the 2nd argument ("toggle"), but I often need 2 functions to force the value to true or false. Having to declare these functions manually make the code very verbose.
Describe the solution you'd like
const [on, enable, disable] = useEnable(true);
return (
<>
<Button onClick={enable}>ON</Button>
<Button onClick={disable}>OFF</Button>
</>
);
This hook could even expose the toggle function:
const [on, enable, disable, toggle] = useEnable(true);
return (
<>
<Button onClick={enable}>ON</Button>
<Button onClick={disable}>OFF</Button>
<Button onClick={toggle}>SWITCH</Button>
</>
);
Existing useToggle could also benefit from this, without breaking change:
const [on, toggle, enable, disable] = useToggle(true);
return (
<>
<Button onClick={enable}>ON</Button>
<Button onClick={disable}>OFF</Button>
<Button onClick={toggle}>SWITCH</Button>
</>
);
Describe alternatives you've considered
I could maintain a custom hook but hey, why not have it in react-use?
toggle(true),toggle(false) very good already?