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

Add createNavigator and not export Screen directly #3

Merged
merged 4 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions example/StackNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NavigationProp,
CommonAction,
ParamListBase,
Router,
Router, createNavigator,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't prettier throw here 🤔

} from '../src/index';

type Props = {
Expand Down Expand Up @@ -224,7 +224,7 @@ const StackRouter: Router<CommonAction | Action> = {
},
};

export default function StackNavigator(props: Props) {
export default createNavigator((props: Props) => {
const { navigation, descriptors } = useNavigationBuilder(StackRouter, props);

return (
Expand Down Expand Up @@ -267,4 +267,4 @@ export default function StackNavigator(props: Props) {
</div>
</div>
);
}
})
5 changes: 3 additions & 2 deletions example/TabNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CommonAction,
ParamListBase,
Router,
createNavigator
} from '../src/index';

type Props = {
Expand Down Expand Up @@ -143,7 +144,7 @@ const TabRouter: Router<Action | CommonAction> = {
},
};

export default function TabNavigator(props: Props) {
export default createNavigator((props: Props) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better to do export default createNavigator(TabNavigator) below so this component has a display name. Otherwise it'll show up as Unnamed in dev tools and anonymous in stack traces.

const { navigation, descriptors } = useNavigationBuilder(TabRouter, props);

return (
Expand All @@ -163,4 +164,4 @@ export default function TabNavigator(props: Props) {
))}
</div>
);
}
})
16 changes: 2 additions & 14 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import * as React from 'react';
import { render } from 'react-dom';
import {
NavigationContainer,
Screen,
CompositeNavigationProp,
TypedNavigator,
NavigationHelpers,
PartialState,
} from '../src';
import StackNavigator, { StackNavigationProp } from './StackNavigator';
import TabNavigator, { TabNavigationProp } from './TabNavigator';
import Stack, { StackNavigationProp } from './StackNavigator';
import Tab, { TabNavigationProp } from './TabNavigator';

type StackParamList = {
first: { author: string };
Expand All @@ -22,16 +20,6 @@ type TabParamList = {
fifth: undefined;
};

const Stack: TypedNavigator<StackParamList, typeof StackNavigator> = {
satya164 marked this conversation as resolved.
Show resolved Hide resolved
Navigator: StackNavigator,
Screen,
};

const Tab: TypedNavigator<TabParamList, typeof TabNavigator> = {
Navigator: TabNavigator,
Screen,
};

const First = ({
navigation,
}: {
Expand Down
14 changes: 14 additions & 0 deletions src/createNavigator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from 'react';
import { ParamListBase, ScreenProps, TypedNavigator } from './types';
import Screen from './Screen';

export default function createNavigator<ParamList extends ParamListBase>(
Navigator: React.ComponentType<any>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to use generic for this for types to work properly

<ParamList extends ParamListBase, N extends React.ComponentType<any>>(Navigator: N): TypedNavigator<ParamList, N>

Though since this function is called by navigator now, we need to change it to not include the param list stuff.

): TypedNavigator<ParamList, typeof Navigator> {
return {
Navigator,
Screen: Screen as React.ComponentType<
ScreenProps<ParamList, keyof ParamList>
>,
};
}
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { default as NavigationContainer } from './NavigationContainer';
export { default as Screen } from './Screen';
export { default as createNavigator } from './createNavigator';

export { default as useNavigationBuilder } from './useNavigationBuilder';

Expand Down