You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
680 B

/**
* Create a state setter pair for a boolean value that can be "switched".
* Unlike `useState(false)`, `useToggleState` will automatically flip the state
* value when its setter is called with no argument.
*
* @param initialState The initial boolean value
* @returns A tuple of the current state and a setter
*
* ```jsx
* const [show, toggleShow] = useToggleState(false)
*
* return (
* <>
* <button onClick={() => toggleShow()}>
* Toggle
* <button>
*
* {show && <strong>Now you can see me</strong>}
* </>
* )
*
* ```
*/
export default function useToggleState(initialState?: boolean): [boolean, (value?: boolean | undefined) => void];