Skip to content

Commit

Permalink
fix: clear options set from a screen when it unmounts. closes #9756
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jul 28, 2021
1 parent 51b636d commit d2d7f8d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/core/src/SceneView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Props<State extends NavigationState, ScreenOptions extends {}> = {
getState: () => State;
setState: (state: State) => void;
options: object;
clearOptions: () => void;
};

/**
Expand All @@ -37,6 +38,7 @@ export default function SceneView<
getState,
setState,
options,
clearOptions,
}: Props<State, ScreenOptions>) {
const navigatorKeyRef = React.useRef<string | undefined>();
const getKey = React.useCallback(() => navigatorKeyRef.current, []);
Expand Down Expand Up @@ -78,6 +80,12 @@ export default function SceneView<
isInitialRef.current = false;
});

// Clear options set by this screen when it is unmounted
React.useEffect(() => {
return clearOptions;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const getIsInitial = React.useCallback(() => isInitialRef.current, []);

const context = React.useMemo(
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/useDescriptors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ export default function useDescriptors<
...customOptions,
};

const clearOptions = () =>
setOptions((o) => {
if (route.key in o) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { [route.key]: _, ...rest } = o;
return rest;
}

return o;
});

acc[route.key] = {
route,
// @ts-expect-error: it's missing action helpers, fix later
Expand All @@ -203,6 +214,7 @@ export default function useDescriptors<
getState={getState}
setState={setState}
options={mergedOptions}
clearOptions={clearOptions}
/>
</NavigationRouteContext.Provider>
</NavigationContext.Provider>
Expand Down

0 comments on commit d2d7f8d

Please sign in to comment.