diff --git a/.changeset/six-pugs-impress.md b/.changeset/six-pugs-impress.md new file mode 100644 index 000000000..0003e6852 --- /dev/null +++ b/.changeset/six-pugs-impress.md @@ -0,0 +1,5 @@ +--- +"mobx": patch +--- + +Prevent `reaction` from heeping a Reference to the OldValue that would prevent GC. diff --git a/packages/mobx/src/api/autorun.ts b/packages/mobx/src/api/autorun.ts index ddf96ed3f..4843ad789 100644 --- a/packages/mobx/src/api/autorun.ts +++ b/packages/mobx/src/api/autorun.ts @@ -139,7 +139,6 @@ export function reaction( let firstTime = true let isScheduled = false let value: T - let oldValue: T | undefined const equals: IEqualsComparer = (opts as any).compareStructural ? comparer.structural @@ -165,10 +164,10 @@ export function reaction( return } let changed: boolean = false + const oldValue = value r.track(() => { const nextValue = allowStateChanges(false, () => expression(r)) changed = firstTime || !equals(value, nextValue) - oldValue = value value = nextValue })