Skip to content

Commit 4acdb34

Browse files
committed
feat: use useInsertionEffect instead of useLayoutEffect
This avoids an issue if the callback is passed down to a child and called in an effect. Since effects are called in child before parent, the callback won't be up-to-date. Using `useInsertionEffect` minimizes this as insetion effects are called before `useLayoutEffect`.
1 parent 2fe9917 commit 4acdb34

File tree

1 file changed

+1
-10
lines changed

1 file changed

+1
-10
lines changed

src/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import * as React from 'react';
22

3-
/**
4-
* Use `useEffect` during SSR and `useLayoutEffect` in the Browser & React Native to avoid warnings.
5-
*/
6-
const useClientLayoutEffect =
7-
typeof document !== 'undefined' ||
8-
(typeof navigator !== 'undefined' && navigator.product === 'ReactNative')
9-
? React.useLayoutEffect
10-
: React.useEffect;
11-
123
/**
134
* React hook which returns the latest callback without changing the reference.
145
*/
@@ -23,7 +14,7 @@ function useLatestCallback<T extends Function>(callback: T): T {
2314
return ref.current.apply(this, args);
2415
} as unknown as T).current;
2516

26-
useClientLayoutEffect(() => {
17+
React.useInsertionEffect(() => {
2718
ref.current = callback;
2819
});
2920

0 commit comments

Comments
 (0)