Skip to content

Instantly share code, notes, and snippets.

@isNan909
Created October 15, 2022 06:39
Show Gist options
  • Select an option

  • Save isNan909/37673d1a23ab719efcc74e9efb99a566 to your computer and use it in GitHub Desktop.

Select an option

Save isNan909/37673d1a23ab719efcc74e9efb99a566 to your computer and use it in GitHub Desktop.
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