Last active
July 4, 2022 03:23
-
-
Save xeniode/3f87d7fa76a2452236078cf9a6eebbed to your computer and use it in GitHub Desktop.
[Custom React Hook - useToggle] A custom react hook to toggle between true or false #react #javascript #reactHooks
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 } from "react"; | |
export default function useToggle(defaultValue){ | |
const [value, setValue] = useState(defaultValue); | |
function toggleValue(value) { | |
setValue(currValue => typeof value === "boolean" ? value : !currValue) | |
} | |
return [value, toggleValue] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment