function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}
const useToggle = (onToggle) => {
const [state, setState] = useState(false);
const preState = usePrevious(state);
const toggle = useCallback(() => setState(state => !state), []);
useEffect(() => {
if (preState !== undefined && preState !== state) {
onToggle(state);
}
}, [state]);
return [state, toggle];
}