generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathuseCompatibleInsertionEffect.tsx
48 lines (43 loc) · 1.38 KB
/
useCompatibleInsertionEffect.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// import canUseDom from 'rc-util/lib/Dom/canUseDom';
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
import type { EffectCallback } from 'react';
import * as React from 'react';
// We need fully clone React function here
// to avoid webpack warning React 17 do not export `useId`
const fullClone = {
...React,
};
const { useInsertionEffect } = fullClone;
type UseCompatibleInsertionEffect = (
renderEffect: EffectCallback,
effect: (polyfill?: boolean) => ReturnType<EffectCallback>,
deps: React.DependencyList,
) => void;
/**
* Polyfill `useInsertionEffect` for React < 18
* @param renderEffect will be executed in `useMemo`, and do not have callback
* @param effect will be executed in `useLayoutEffect`
* @param deps
*/
const useInsertionEffectPolyfill: UseCompatibleInsertionEffect = (
renderEffect,
effect,
deps,
) => {
React.useMemo(renderEffect, deps);
useLayoutEffect(() => effect(true), deps);
};
/**
* Compatible `useInsertionEffect`
* will use `useInsertionEffect` if React version >= 18,
* otherwise use `useInsertionEffectPolyfill`.
*/
const useCompatibleInsertionEffect: UseCompatibleInsertionEffect =
useInsertionEffect
? (renderEffect, effect, deps) =>
useInsertionEffect(() => {
renderEffect();
return effect();
}, deps)
: useInsertionEffectPolyfill;
export default useCompatibleInsertionEffect;