Created
May 26, 2022 22:01
-
-
Save isaobushi/e1527d133e80389909c39866e3a6d721 to your computer and use it in GitHub Desktop.
Custom hooks #hooks #toggle
This file contains 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 } from "react" | |
export default function useToggle(defaultValue) { | |
const [value, setValue] = useState(defaultValue); | |
function toggleValue(value) { | |
setValue(currentValue => | |
typeof value === "boolean" ? value : !currentValue | |
) | |
} | |
return [value, toggleValue] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment