react-use/src/useToggle.ts
2020-04-08 09:51:22 +03:00

10 lines
349 B
TypeScript

import { useReducer, Reducer } from 'react';
const toggleReducer = (state: boolean, nextValue?: any) => (typeof nextValue === 'boolean' ? nextValue : !state);
const useToggle = (initialValue: boolean): [boolean, (nextValue?: any) => void] => {
return useReducer<Reducer<boolean, any>>(toggleReducer, initialValue);
};
export default useToggle;