Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

Commit adc2fe4

Browse files
committed
fix: throw when duplicate screens are defined
1 parent 71f4ef1 commit adc2fe4

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

packages/core/src/__tests__/index.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,24 @@ it("doesn't throw when direct children is Screen or empty element", () => {
617617
</NavigationContainer>
618618
);
619619
});
620+
621+
it('throws when multiple screens with same name are defined', () => {
622+
const TestNavigator = (props: any) => {
623+
useNavigationBuilder(MockRouter, props);
624+
return null;
625+
};
626+
627+
const element = (
628+
<NavigationContainer>
629+
<TestNavigator>
630+
<Screen name="foo" component={jest.fn()} />
631+
<Screen name="bar" component={jest.fn()} />
632+
<Screen name="foo" component={jest.fn()} />
633+
</TestNavigator>
634+
</NavigationContainer>
635+
);
636+
637+
expect(() => render(element).update(element)).toThrowError(
638+
"A navigator cannot contain multiple 'Screen' components with the same name (found duplicate screen named 'foo')"
639+
);
640+
});

packages/core/src/useNavigationBuilder.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ export default function useNavigationBuilder<
8181

8282
const screens = getRouteConfigsFromChildren<ScreenOptions>(children).reduce(
8383
(acc, curr) => {
84+
if (curr.name in acc) {
85+
throw new Error(
86+
`A navigator cannot contain multiple 'Screen' components with the same name (found duplicate screen named '${curr.name}')`
87+
);
88+
}
89+
8490
acc[curr.name] = curr;
8591
return acc;
8692
},

0 commit comments

Comments
 (0)