UNPKG

@wojtekmaj/react-hooks

Version:

A collection of React Hooks.

13 lines (12 loc) 414 B
import { 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) { const [value, setValue] = useState(defaultValue); const toggleValue = () => setValue((prevValue) => !prevValue); return [value, toggleValue]; }