mirror of
https://github.com/re-rxjs/react-rxjs.git
synced 2025-12-08 18:01:51 +00:00
fix(core): ensure the same observable on useObservable
This commit is contained in:
parent
c01cd43cb0
commit
ce05cf36b4
@ -80,7 +80,7 @@ export default function connectFactoryObservable<A extends [], O>(
|
||||
return [
|
||||
(...input: A) => {
|
||||
const [source$, getValue] = getSharedObservables$(input)
|
||||
return useObservable(source$, getValue)
|
||||
return useObservable(source$, getValue, input)
|
||||
},
|
||||
(...input: A) => getSharedObservables$(input)[0],
|
||||
]
|
||||
|
||||
@ -18,9 +18,11 @@ import { useObservable } from "../internal/useObservable"
|
||||
* subscription, then the hook will leverage React Suspense while it's waiting
|
||||
* for the first value.
|
||||
*/
|
||||
const emptyArr: Array<any> = []
|
||||
export default function connectObservable<T>(observable: Observable<T>) {
|
||||
const sharedObservable$ = shareLatest<T>(observable, false)
|
||||
const getValue = reactEnhancer(sharedObservable$)
|
||||
const useStaticObservable = () => useObservable(sharedObservable$, getValue)
|
||||
const useStaticObservable = () =>
|
||||
useObservable(sharedObservable$, getValue, emptyArr)
|
||||
return [useStaticObservable, sharedObservable$] as const
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import { Observable } from "rxjs"
|
||||
export const useObservable = <O>(
|
||||
source$: Observable<O>,
|
||||
getValue: () => O,
|
||||
keys: Array<any>,
|
||||
): Exclude<O, typeof SUSPENSE> => {
|
||||
const [state, setState] = useState(getValue)
|
||||
const prevStateRef = useRef<O | (() => O)>(state)
|
||||
@ -37,7 +38,7 @@ export const useObservable = <O>(
|
||||
t.unsubscribe()
|
||||
|
||||
return () => subscription.unsubscribe()
|
||||
}, [source$])
|
||||
}, keys)
|
||||
|
||||
return state as Exclude<O, typeof SUSPENSE>
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user