react-use icon indicating copy to clipboard operation
react-use copied to clipboard

New hook: useEnable?

Open damrem opened this issue 3 years ago • 1 comments

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?

damrem avatar Jun 01 '22 17:06 damrem

toggle(true),toggle(false) very good already?

childrentime avatar Sep 19 '22 04:09 childrentime