diff --git a/react-migration-toolkit/src/react/contexts/application-context.tsx b/react-migration-toolkit/src/react/contexts/application-context.tsx index 5d82ecc..4db42fa 100644 --- a/react-migration-toolkit/src/react/contexts/application-context.tsx +++ b/react-migration-toolkit/src/react/contexts/application-context.tsx @@ -1,4 +1,17 @@ -import { createContext, type ReactNode, type PropsWithChildren } from 'react'; +import { + createContext, + type ReactNode, + type PropsWithChildren, + useState, + useEffect, +} from 'react'; +import { + createContext, + type ReactNode, + type PropsWithChildren, + useState, + useEffect, +} from 'react'; import type ApplicationInstance from '@ember/application/instance'; interface ApplicationProviderProps extends PropsWithChildren { @@ -13,6 +26,20 @@ export function ApplicationProvider({ owner, children, }: ApplicationProviderProps): ReactNode { + const [isDestroyed, setIsDestroyed] = useState(false); + + useEffect(() => { + const oldWillDestroy = owner.willDestroy; + owner.willDestroy = (...args) => { + setIsDestroyed(true); + oldWillDestroy.apply(owner, args); + }; + }, [owner]); + + if (isDestroyed) { + return null; + } + return ( {children}