Skip to content

Instantly share code, notes, and snippets.

@xeniode
Last active July 4, 2022 03:23
Show Gist options
  • Save xeniode/3f87d7fa76a2452236078cf9a6eebbed to your computer and use it in GitHub Desktop.
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
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