Skip to content

Commit 2dbf833

Browse files
authored
Use abort controller to prevent async setState after unmount. (#463)
1 parent 7121c42 commit 2dbf833

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Modules/@babylonjs/react-native/NativeEngineHook.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ export function useModuleInitializer(): boolean | undefined {
99
const [initialized, setInitialized] = useState<boolean>();
1010

1111
useEffect(() => {
12+
const abortController = new AbortController();
1213
(async () => {
13-
setInitialized(await ensureInitialized());
14+
const isInitialized = await ensureInitialized();
15+
16+
if (!abortController.signal.aborted) {
17+
setInitialized(isInitialized);
18+
}
1419
})();
20+
21+
return () => {
22+
abortController.abort();
23+
}
1524
}, []);
1625

1726
return initialized;

0 commit comments

Comments
 (0)