mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-04-26 13:35:32 +08:00
fix: fix getCurrentOptions for nested screens
This commit is contained in:
@@ -1529,7 +1529,7 @@ it('returns currently focused route with getCurrentRoute', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("returns currently focused route's options with getCurrentOptions", () => {
|
||||
it("returns focused screen's options with getCurrentOptions when focused screen is rendered", () => {
|
||||
const TestNavigator = (props: any): any => {
|
||||
const { state, descriptors } = useNavigationBuilder(MockRouter, props);
|
||||
|
||||
@@ -1554,6 +1554,11 @@ it("returns currently focused route's options with getCurrentOptions", () => {
|
||||
component={TestScreen}
|
||||
options={{ sample: 'data' }}
|
||||
/>
|
||||
<Screen
|
||||
name="bar-b"
|
||||
component={TestScreen}
|
||||
options={{ sample3: 'data' }}
|
||||
/>
|
||||
</TestNavigator>
|
||||
)}
|
||||
</Screen>
|
||||
@@ -1568,6 +1573,66 @@ it("returns currently focused route's options with getCurrentOptions", () => {
|
||||
sample: 'data',
|
||||
sample2: 'data',
|
||||
});
|
||||
|
||||
act(() => navigation.current?.navigate('bar-b'));
|
||||
|
||||
expect(navigation.current?.getCurrentOptions()).toEqual({
|
||||
sample2: 'data',
|
||||
sample3: 'data',
|
||||
});
|
||||
});
|
||||
|
||||
it("returns focused screen's options with getCurrentOptions when all screens are rendered", () => {
|
||||
const TestNavigator = (props: any): any => {
|
||||
const { state, descriptors } = useNavigationBuilder(MockRouter, props);
|
||||
|
||||
return <>{state.routes.map((route) => descriptors[route.key].render())}</>;
|
||||
};
|
||||
|
||||
const TestScreen = () => null;
|
||||
|
||||
const navigation = React.createRef<NavigationContainerRef>();
|
||||
|
||||
const container = (
|
||||
<BaseNavigationContainer ref={navigation}>
|
||||
<TestNavigator>
|
||||
<Screen name="bar" options={{ a: 'b' }}>
|
||||
{() => (
|
||||
<TestNavigator
|
||||
initialRouteName="bar-a"
|
||||
screenOptions={() => ({ sample2: 'data' })}
|
||||
>
|
||||
<Screen
|
||||
name="bar-a"
|
||||
component={TestScreen}
|
||||
options={{ sample: 'data' }}
|
||||
/>
|
||||
<Screen
|
||||
name="bar-b"
|
||||
component={TestScreen}
|
||||
options={{ sample3: 'data' }}
|
||||
/>
|
||||
</TestNavigator>
|
||||
)}
|
||||
</Screen>
|
||||
<Screen name="xux" component={TestScreen} />
|
||||
</TestNavigator>
|
||||
</BaseNavigationContainer>
|
||||
);
|
||||
|
||||
render(container).update(container);
|
||||
|
||||
expect(navigation.current?.getCurrentOptions()).toEqual({
|
||||
sample: 'data',
|
||||
sample2: 'data',
|
||||
});
|
||||
|
||||
act(() => navigation.current?.navigate('bar-b'));
|
||||
|
||||
expect(navigation.current?.getCurrentOptions()).toEqual({
|
||||
sample2: 'data',
|
||||
sample3: 'data',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not throw if while getting current options with no options defined', () => {
|
||||
|
||||
@@ -16,7 +16,7 @@ export default function useOptionsGetters({
|
||||
setNumberOfChildrenListeners,
|
||||
] = React.useState(0);
|
||||
const optionsGettersFromChild = React.useRef<
|
||||
Record<string, (() => object | undefined | null) | undefined>
|
||||
Record<string, () => object | undefined | null>
|
||||
>({});
|
||||
|
||||
const { addOptionsGetter: parentAddOptionsGetter } = React.useContext(
|
||||
@@ -63,7 +63,8 @@ export default function useOptionsGetters({
|
||||
|
||||
return () => {
|
||||
setNumberOfChildrenListeners((prev) => prev - 1);
|
||||
optionsGettersFromChild.current[key] = undefined;
|
||||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
||||
delete optionsGettersFromChild.current[key];
|
||||
};
|
||||
},
|
||||
[]
|
||||
|
||||
Reference in New Issue
Block a user