Created
October 15, 2022 06:39
-
-
Save isNan909/37673d1a23ab719efcc74e9efb99a566 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} from "react"; | |
| function useToggle(initialState){ | |
| const [state, setState] = useState(initialState || false); | |
| return [state, () => setState(!state)]; | |
| }; | |
| //Use it | |
| // const [on, toggle] = useToggle(); | |
| // return <button onClick={toggle}>Turn {on ? "off" : "on"}</button>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment