Skip to content

Instantly share code, notes, and snippets.

@GalindoSVQ
Created October 31, 2023 06:03
Show Gist options
  • Select an option

  • Save GalindoSVQ/2637e3c970e65de74d4c79c40583b72c to your computer and use it in GitHub Desktop.

Select an option

Save GalindoSVQ/2637e3c970e65de74d4c79c40583b72c to your computer and use it in GitHub Desktop.
import { useState, useCallback } from 'react';
export default function useToggle(initialValue: boolean = true) {
const [value, setValue] = useState(Boolean(initialValue));
const handleValue = useCallback(
(newValue: any) =>
typeof newValue === 'boolean'
? setValue(newValue)
: setValue((currentValue) => !currentValue),
[]
);
return [value, handleValue];
}
@GalindoSVQ

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment