Created
October 31, 2023 06:03
-
-
Save GalindoSVQ/2637e3c970e65de74d4c79c40583b72c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stackblitz link: https://stackblitz.com/edit/vitejs-vite-ckfamw?file=src%2FuseToggle.ts