Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
432 B · 13 lines
TypeScript
at main
1234567891011121314import { useState } from 'react';
/** * Returns a flag and a function to toggle it. * * @param {boolean} defaultValue Default value * @returns {[boolean, () => void]} Flag and toggle function */export default function useToggle(defaultValue = false): [boolean, () => void] { const [value, setValue] = useState(defaultValue); const toggleValue = () => setValue((prevValue) => !prevValue); return [value, toggleValue];}