Skip to content

Commit

Permalink
fix: adapt hooks to work in strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rozhkovs committed Sep 28, 2023
1 parent 19b4cea commit 1c3299f
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src/useInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {useMemo} from 'react';
import {EMPTY_ARRAY} from 'default-values';

const useInit = <T>(creator: () => T) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(creator, EMPTY_ARRAY);
return useMemo(creator, EMPTY_ARRAY); // eslint-disable-line react-hooks/exhaustive-deps
};

export default useInit;
9 changes: 4 additions & 5 deletions src/useIsFirstRender.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {useRef} from 'react';
import {useEffect, useRef} from 'react';

const useIsFirstRender = () => {
const isFirst = useRef(true);
if (isFirst.current) {
useEffect(() => {
isFirst.current = false;
return true;
}
return false;
});
return isFirst.current;
};

export default useIsFirstRender;
3 changes: 1 addition & 2 deletions src/useMemoArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import useChangeCounter from './useChangeCounter';

const useMemoArray = <T extends readonly any[]>(arr: T): T => {
const depsVal = useChangeCounter(arr, isEqual);
// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(() => arr, [depsVal]);
return useMemo(() => arr, [depsVal]); // eslint-disable-line react-hooks/exhaustive-deps
};

export default useMemoArray;
3 changes: 1 addition & 2 deletions src/useMemoObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import useChangeCounter from './useChangeCounter';

const useMemoObject = <T extends object>(obj: T) => {
const depsVal = useChangeCounter(Object.values(obj), isEqual);
// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(() => obj, [depsVal]);
return useMemo(() => obj, [depsVal]); // eslint-disable-line react-hooks/exhaustive-deps
};

export default useMemoObject;
9 changes: 5 additions & 4 deletions src/usePrevious.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {useRef} from 'react';
import {useEffect, useRef} from 'react';

const usePrevious = <T>(value: T): T | undefined => {
const ref = useRef<T | undefined>(undefined);
const temp = ref.current;
ref.current = value;
return temp;
useEffect(() => {
ref.current = value;
});
return ref.current;
};

export default usePrevious;
3 changes: 1 addition & 2 deletions src/useStableCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const useStableCallback = <T extends AnyFunc | null | undefined>(
ref.current = callback;
return useCallback<any>(
(...params: any[]) => ref.current?.(...params),
// eslint-disable-next-line react-hooks/exhaustive-deps
EMPTY_ARRAY,
EMPTY_ARRAY, // eslint-disable-line react-hooks/exhaustive-deps
);
};

Expand Down
3 changes: 1 addition & 2 deletions src/useStateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ function useStateRef<S>(initialState: S | (() => S) | undefined) {
ref.current = typeof val === 'function' ? (val as any)(ref.current) : val;
setState(ref.current);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
EMPTY_ARRAY,
EMPTY_ARRAY, // eslint-disable-line react-hooks/exhaustive-deps
);

return [state, dispatch, ref];
Expand Down

0 comments on commit 1c3299f

Please sign in to comment.