|
1 | 1 | import React from 'react' |
2 | 2 |
|
3 | | -import { uid, isServer } from '../core/utils' |
| 3 | +import { isServer } from '../core/utils' |
4 | 4 | import { QueryResultBase, BaseQueryConfig, QueryStatus } from '../core/types' |
5 | 5 |
|
6 | | -export function useUid(): number { |
7 | | - const ref = React.useRef(0) |
8 | | - |
9 | | - if (ref.current === null) { |
10 | | - ref.current = uid() |
11 | | - } |
12 | | - |
13 | | - return ref.current |
14 | | -} |
15 | | - |
16 | 6 | export function useGetLatest<T>(obj: T): () => T { |
17 | 7 | const ref = React.useRef<T>(obj) |
18 | 8 | ref.current = obj |
19 | 9 | return React.useCallback(() => ref.current, []) |
20 | 10 | } |
21 | 11 |
|
22 | | -export function useMountedCallback<T extends Function>(callback: T): T { |
23 | | - const mounted = React.useRef(false) |
| 12 | +function useIsMounted(): () => boolean { |
| 13 | + const mountedRef = React.useRef(false) |
| 14 | + const isMounted = React.useCallback(() => mountedRef.current, []) |
24 | 15 |
|
25 | 16 | React[isServer ? 'useEffect' : 'useLayoutEffect'](() => { |
26 | | - mounted.current = true |
| 17 | + mountedRef.current = true |
27 | 18 | return () => { |
28 | | - mounted.current = false |
| 19 | + mountedRef.current = false |
29 | 20 | } |
30 | 21 | }, []) |
31 | 22 |
|
| 23 | + return isMounted |
| 24 | +} |
| 25 | + |
| 26 | +export function useMountedCallback<T extends Function>(callback: T): T { |
| 27 | + const isMounted = useIsMounted() |
32 | 28 | return (React.useCallback( |
33 | | - (...args: any[]) => (mounted.current ? callback(...args) : void 0), |
34 | | - [callback] |
| 29 | + (...args: any[]) => (isMounted() ? callback(...args) : void 0), |
| 30 | + [callback, isMounted] |
35 | 31 | ) as any) as T |
36 | 32 | } |
37 | 33 |
|
| 34 | +/** |
| 35 | + * This hook is a safe useState version which schedules state updates in microtasks |
| 36 | + * to prevent updating a component state while React is rendering parent components |
| 37 | + * or when the component is not mounted anymore. |
| 38 | + */ |
| 39 | +export function useSafeState<S>( |
| 40 | + initialState: S | (() => S) |
| 41 | +): [S, React.Dispatch<React.SetStateAction<S>>] { |
| 42 | + const isMounted = useIsMounted() |
| 43 | + const [state, setState] = React.useState(initialState) |
| 44 | + |
| 45 | + const safeSetState = React.useCallback( |
| 46 | + (value: React.SetStateAction<S>) => { |
| 47 | + scheduleMicrotask(() => { |
| 48 | + if (isMounted()) { |
| 49 | + setState(value) |
| 50 | + } |
| 51 | + }) |
| 52 | + }, |
| 53 | + [isMounted] |
| 54 | + ) |
| 55 | + |
| 56 | + return [state, safeSetState] |
| 57 | +} |
| 58 | + |
38 | 59 | export function useRerenderer() { |
39 | | - const rerender = useMountedCallback(React.useState<unknown>()[1]) |
40 | | - return React.useCallback(() => rerender({}), [rerender]) |
| 60 | + const [, setState] = useSafeState({}) |
| 61 | + return React.useCallback(() => setState({}), [setState]) |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Schedules a microtask. |
| 66 | + * This can be useful to prevent updating state while rendering. |
| 67 | + */ |
| 68 | +function scheduleMicrotask(fn: () => void): void { |
| 69 | + Promise.resolve().then(fn) |
41 | 70 | } |
42 | 71 |
|
43 | 72 | export function handleSuspense( |
|
0 commit comments