Skip to content

Commit 16b510e

Browse files
authored
Merge pull request #458 from welkinwong/feature/4.0.1
fix(useTrackerSuspense): fix unmount error in StrictMode (dev only)
2 parents 931ee40 + 65fb349 commit 16b510e

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

packages/react-meteor-data/suspense/useTracker.tests.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,29 @@ Meteor.isClient &&
428428
);
429429
}
430430
);
431+
432+
Meteor.isClient &&
433+
runForVariants(
434+
'suspense/useTracker - component unmount in Strict Mode',
435+
async function (test, useTrackerFn) {
436+
const { simpleFetch } = setupTest();
437+
438+
const Test = () => {
439+
useTrackerFn('TestDocs', simpleFetch);
440+
441+
return null;
442+
};
443+
444+
const { queryByText, findByText, unmount } = render(<Test />, {
445+
container: document.createElement('container'),
446+
wrapper: TestSuspense,
447+
reactStrictMode: true,
448+
});
449+
450+
await new Promise((resolve) => setTimeout(resolve, 100));
451+
452+
unmount();
453+
454+
test.isTrue(true, 'should handle unmount correctly in Strict Mode');
455+
}
456+
);

packages/react-meteor-data/suspense/useTracker.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,11 @@ export function useTrackerSuspenseNoDeps<T = any>(key: string, reactiveFn: IReac
131131

132132
// stop the computation on unmount
133133
return () => {
134-
refs.computation?.stop()
135-
delete refs.computation
134+
if (refs.computation) {
135+
refs.computation.stop()
136+
delete refs.computation
137+
}
138+
136139
refs.isMounted = false
137140
}
138141
}, [])
@@ -192,8 +195,11 @@ export const useTrackerSuspenseWithDeps =
192195
refs.isMounted = true
193196

194197
return () => {
195-
refs.computation.stop()
196-
delete refs.computation
198+
if (refs.computation) {
199+
refs.computation.stop()
200+
delete refs.computation
201+
}
202+
197203
refs.isMounted = false
198204
}
199205
}, deps)

0 commit comments

Comments
 (0)