use-toggle.ts 542 B

1234567891011121314151617181920
  1. import React from "react";
  2. // @see https://usehooks.com/useToggle
  3. const useToggle = (initialState: boolean = false): [boolean, any] => {
  4. // Initialize the state
  5. const [state, setState] = React.useState<boolean>(initialState)
  6. // Define and memorize toggler function in case we pass down the comopnent,
  7. // This function change the boolean value to it's opposite value
  8. const toggle = React.useCallback(
  9. (): void => setState((state) => !state),
  10. []
  11. )
  12. return [state, toggle]
  13. }
  14. export default useToggle