Skip to content

Commit 7226765

Browse files
committed
fix: hide app context when owner is destroyed
1 parent e716ad0 commit 7226765

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

react-migration-toolkit/src/react/contexts/application-context.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
import { createContext, type ReactNode, type PropsWithChildren } from 'react';
1+
import {
2+
createContext,
3+
type ReactNode,
4+
type PropsWithChildren,
5+
useState,
6+
useEffect,
7+
} from 'react';
8+
import {
9+
createContext,
10+
type ReactNode,
11+
type PropsWithChildren,
12+
useState,
13+
useEffect,
14+
} from 'react';
215
import type ApplicationInstance from '@ember/application/instance';
316

417
interface ApplicationProviderProps extends PropsWithChildren {
@@ -13,6 +26,20 @@ export function ApplicationProvider({
1326
owner,
1427
children,
1528
}: ApplicationProviderProps): ReactNode {
29+
const [isDestroyed, setIsDestroyed] = useState<boolean>(false);
30+
31+
useEffect(() => {
32+
const oldWillDestroy = owner.willDestroy;
33+
owner.willDestroy = (...args) => {
34+
setIsDestroyed(true);
35+
oldWillDestroy.apply(owner, args);
36+
};
37+
}, [owner]);
38+
39+
if (isDestroyed) {
40+
return null;
41+
}
42+
1643
return (
1744
<ApplicationContext.Provider value={owner}>
1845
{children}

0 commit comments

Comments
 (0)