-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): fix useAttach not work with react18 strict mode (#3284)
- Loading branch information
Showing
9 changed files
with
121 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
import { useLayoutEffect, useMemo } from 'react' | ||
import { unstable_useCompatFactory } from '@formily/reactive-react' | ||
import { Form } from '@formily/core' | ||
import { uid } from '@formily/shared' | ||
import { useForm } from './useForm' | ||
|
||
export const useFormEffects = (effects?: (form: Form) => void) => { | ||
const form = useForm() | ||
const ref = useMemo(() => { | ||
unstable_useCompatFactory(() => { | ||
const id = uid() | ||
form.addEffects(id, effects) | ||
const request = setTimeout(() => { | ||
form.removeEffects(id) | ||
}, 100) | ||
return { id, request } | ||
}, []) | ||
useLayoutEffect(() => { | ||
clearTimeout(ref.request) | ||
return () => { | ||
form.removeEffects(ref.id) | ||
return { | ||
dispose() { | ||
form.removeEffects(id) | ||
}, | ||
} | ||
}, []) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
export * from './useForceUpdate' | ||
export * from './useObserver' | ||
import { useForceUpdate } from './useForceUpdate' | ||
import { useCompatEffect } from './useCompatEffect' | ||
import { useCompatFactory } from './useCompatFactory' | ||
import { useDidUpdate } from './useDidUpdate' | ||
import { useLayoutEffect } from './useLayoutEffect' | ||
import { useObserver } from './useObserver' | ||
|
||
export const unstable_useForceUpdate = useForceUpdate | ||
export const unstable_useCompatEffect = useCompatEffect | ||
export const unstable_useCompatFactory = useCompatFactory | ||
export const unstable_useDidUpdate = useDidUpdate | ||
export const unstable_useLayoutEffect = useLayoutEffect | ||
export const unstable_useObserver = useObserver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useEffect, useRef, EffectCallback, DependencyList } from 'react' | ||
import { immediate } from '../shared' | ||
|
||
const isArr = Array.isArray | ||
|
||
const isEqualDeps = (target: any, source: any) => { | ||
const arrA = isArr(target) | ||
const arrB = isArr(source) | ||
if (arrA !== arrB) return false | ||
if (arrA) { | ||
if (target.length !== source.length) return false | ||
return target.every((val, index) => val === source[index]) | ||
} | ||
return target === source | ||
} | ||
|
||
export const useCompatEffect = ( | ||
effect: EffectCallback, | ||
deps?: DependencyList | ||
) => { | ||
const depsRef = useRef<DependencyList>(null) | ||
const mountedRef = useRef(false) | ||
useEffect(() => { | ||
mountedRef.current = true | ||
const dispose = effect() | ||
return () => { | ||
mountedRef.current = false | ||
if (!isEqualDeps(depsRef.current, deps)) { | ||
if (dispose) dispose() | ||
return | ||
} | ||
immediate(() => { | ||
if (mountedRef.current) return | ||
if (dispose) dispose() | ||
}) | ||
} | ||
}, deps) | ||
depsRef.current = deps | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from 'react' | ||
import { GarbageCollector } from '../shared' | ||
import { useCompatEffect } from './useCompatEffect' | ||
|
||
class ObjectToBeRetainedByReact {} | ||
|
||
function objectToBeRetainedByReactFactory() { | ||
return new ObjectToBeRetainedByReact() | ||
} | ||
|
||
export const useCompatFactory = <T extends { dispose: () => void }>( | ||
factory: () => T | ||
): T => { | ||
const instRef = React.useRef<T>(null) | ||
const gcRef = React.useRef<GarbageCollector>() | ||
const [objectRetainedByReact] = React.useState( | ||
objectToBeRetainedByReactFactory | ||
) | ||
if (!instRef.current) { | ||
instRef.current = factory() | ||
} | ||
//StrictMode/ConcurrentMode会导致组件无法正确触发UnMount,所以只能自己做垃圾回收 | ||
if (!gcRef.current) { | ||
gcRef.current = new GarbageCollector(() => { | ||
if (instRef.current) { | ||
instRef.current.dispose() | ||
} | ||
}) | ||
gcRef.current.open(objectRetainedByReact) | ||
} | ||
|
||
useCompatEffect(() => { | ||
gcRef.current.close() | ||
return () => { | ||
if (instRef.current) { | ||
instRef.current.dispose() | ||
instRef.current = null | ||
} | ||
} | ||
}, []) | ||
return instRef.current | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,22 @@ | ||
import React from 'react' | ||
import { Tracker } from '@formily/reactive' | ||
import { GarbageCollector, immediate } from '../shared' | ||
import { IObserverOptions } from '../types' | ||
import { useForceUpdate } from './useForceUpdate' | ||
|
||
class ObjectToBeRetainedByReact {} | ||
|
||
function objectToBeRetainedByReactFactory() { | ||
return new ObjectToBeRetainedByReact() | ||
} | ||
import { useCompatFactory } from './useCompatFactory' | ||
|
||
export const useObserver = <T extends () => any>( | ||
view: T, | ||
options?: IObserverOptions | ||
): ReturnType<T> => { | ||
const forceUpdate = useForceUpdate() | ||
const mountedRef = React.useRef(false) | ||
const trackerRef = React.useRef<Tracker>(null) | ||
const gcRef = React.useRef<GarbageCollector>() | ||
const [objectRetainedByReact] = React.useState( | ||
objectToBeRetainedByReactFactory | ||
const tracker = useCompatFactory( | ||
() => | ||
new Tracker(() => { | ||
if (typeof options?.scheduler === 'function') { | ||
options.scheduler(forceUpdate) | ||
} else { | ||
forceUpdate() | ||
} | ||
}, options?.displayName) | ||
) | ||
if (!trackerRef.current) { | ||
trackerRef.current = new Tracker(() => { | ||
if (typeof options?.scheduler === 'function') { | ||
options.scheduler(forceUpdate) | ||
} else { | ||
forceUpdate() | ||
} | ||
}, options?.displayName) | ||
} | ||
|
||
//StrictMode/ConcurrentMode会导致组件无法正确触发UnMount,所以只能自己做垃圾回收 | ||
if (!gcRef.current) { | ||
gcRef.current = new GarbageCollector(() => { | ||
if (trackerRef.current) { | ||
trackerRef.current.dispose() | ||
} | ||
}) | ||
gcRef.current.open(objectRetainedByReact) | ||
} | ||
|
||
React.useEffect(() => { | ||
const dispose = () => { | ||
if (trackerRef.current && !mountedRef.current) { | ||
trackerRef.current.dispose() | ||
trackerRef.current = null | ||
} | ||
} | ||
mountedRef.current = true | ||
gcRef.current.close() | ||
return () => { | ||
mountedRef.current = false | ||
immediate(dispose) | ||
} | ||
}, []) | ||
return trackerRef.current.track(view) | ||
return tracker.track(view) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './observer' | ||
export * from './hooks' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters