mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-03-06 22:39:41 +08:00
fix: throw when duplicate screens are defined
This commit is contained in:
@@ -617,3 +617,24 @@ it("doesn't throw when direct children is Screen or empty element", () => {
|
||||
</NavigationContainer>
|
||||
);
|
||||
});
|
||||
|
||||
it('throws when multiple screens with same name are defined', () => {
|
||||
const TestNavigator = (props: any) => {
|
||||
useNavigationBuilder(MockRouter, props);
|
||||
return null;
|
||||
};
|
||||
|
||||
const element = (
|
||||
<NavigationContainer>
|
||||
<TestNavigator>
|
||||
<Screen name="foo" component={jest.fn()} />
|
||||
<Screen name="bar" component={jest.fn()} />
|
||||
<Screen name="foo" component={jest.fn()} />
|
||||
</TestNavigator>
|
||||
</NavigationContainer>
|
||||
);
|
||||
|
||||
expect(() => render(element).update(element)).toThrowError(
|
||||
"A navigator cannot contain multiple 'Screen' components with the same name (found duplicate screen named 'foo')"
|
||||
);
|
||||
});
|
||||
|
||||
@@ -81,6 +81,12 @@ export default function useNavigationBuilder<
|
||||
|
||||
const screens = getRouteConfigsFromChildren<ScreenOptions>(children).reduce(
|
||||
(acc, curr) => {
|
||||
if (curr.name in acc) {
|
||||
throw new Error(
|
||||
`A navigator cannot contain multiple 'Screen' components with the same name (found duplicate screen named '${curr.name}')`
|
||||
);
|
||||
}
|
||||
|
||||
acc[curr.name] = curr;
|
||||
return acc;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user