refactor: moved out reducer to improve memory usage

This commit is contained in:
Marco Antonio Ghiani 2020-04-08 08:43:53 +02:00 committed by Anton Zinovyev
parent 893153141a
commit cc57c2aed9

View File

@ -1,10 +1,9 @@
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>>(
(state: boolean, nextValue?: any) => (typeof nextValue === 'boolean' ? nextValue : !state),
initialValue
);
return useReducer<Reducer<boolean, any>>(toggleReducer, initialValue);
};
export default useToggle;