Skip to content

Instantly share code, notes, and snippets.

@isaobushi
Created May 26, 2022 22:01
Show Gist options
  • Save isaobushi/e1527d133e80389909c39866e3a6d721 to your computer and use it in GitHub Desktop.
Save isaobushi/e1527d133e80389909c39866e3a6d721 to your computer and use it in GitHub Desktop.
Custom hooks #hooks #toggle
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