UNPKG

@react-hook/toggle

Version:
22 lines (17 loc) 697 B
import * as React from 'react'; function useToggle( // @ts-ignore off = false, // @ts-ignore on = true, defaultValue = off) { /* istanbul ignore next */ if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') { if (defaultValue !== on && defaultValue !== off) { throw new Error("'defaultValue' must be one of either the 'off' or 'on' value. " + ("'" + defaultValue + "' was not equal to '" + off + "' or '" + on + "'.")); } } const [value, setValue] = React.useState(defaultValue); function _ref(current) { return current === off ? on : off; } return [value, React.useCallback(() => setValue(_ref), [on, off])]; } export default useToggle;