Skip to content

Commit d66acf2

Browse files
committed
fix(use-observable): clear subject when teardown
1 parent 3bc2575 commit d66acf2

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/use-observable.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useState, useEffect, useMemo } from 'react'
44
const PREFIX = '__SUBJECT__'
55

66
const propsSubjects: {
7-
[index: string]: Subject<any>
7+
[index: string]: Subject<any> | null
88
} = {}
99

1010
let subjectId = 0
@@ -50,9 +50,11 @@ export function useObservable<T, U extends ReadonlyArray<any> | undefined>(
5050
const subscription = input$.subscribe((value) => {
5151
setState([value, subjectId])
5252
})
53-
propsSubjects[concatSubjectKey(subjectId)] = props$
53+
const subjectKey = concatSubjectKey(subjectId)
54+
propsSubjects[subjectKey] = props$
5455
return () => {
5556
subscription.unsubscribe()
57+
propsSubjects[subjectKey] = null
5658
}
5759
},
5860
[0], // immutable forever

0 commit comments

Comments
 (0)