diff --git a/example/StackNavigator.tsx b/example/StackNavigator.tsx index 2610f66d..10b365a2 100644 --- a/example/StackNavigator.tsx +++ b/example/StackNavigator.tsx @@ -8,6 +8,7 @@ import { CommonAction, ParamListBase, Router, + createNavigator, } from '../src/index'; type Props = { @@ -167,7 +168,6 @@ const StackRouter: Router = { ], }; } - return null; case 'REPLACE': { @@ -224,7 +224,7 @@ const StackRouter: Router = { }, }; -export default function StackNavigator(props: Props) { +export function StackNavigator(props: Props) { const { navigation, descriptors } = useNavigationBuilder(StackRouter, props); return ( @@ -268,3 +268,5 @@ export default function StackNavigator(props: Props) { ); } + +export default createNavigator(StackNavigator); diff --git a/example/TabNavigator.tsx b/example/TabNavigator.tsx index 5e8b069e..6cde9e7d 100644 --- a/example/TabNavigator.tsx +++ b/example/TabNavigator.tsx @@ -8,6 +8,7 @@ import { CommonAction, ParamListBase, Router, + createNavigator, } from '../src/index'; type Props = { @@ -143,9 +144,8 @@ const TabRouter: Router = { }, }; -export default function TabNavigator(props: Props) { +export function TabNavigator(props: Props) { const { navigation, descriptors } = useNavigationBuilder(TabRouter, props); - return (
{navigation.state.routes.map((route, i, self) => ( @@ -164,3 +164,5 @@ export default function TabNavigator(props: Props) {
); } + +export default createNavigator(TabNavigator); \ No newline at end of file diff --git a/example/index.tsx b/example/index.tsx index 58a89f26..68218589 100644 --- a/example/index.tsx +++ b/example/index.tsx @@ -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 }; @@ -22,15 +20,9 @@ type TabParamList = { fifth: undefined; }; -const Stack: TypedNavigator = { - Navigator: StackNavigator, - Screen, -}; +const MyStack = Stack(); -const Tab: TypedNavigator = { - Navigator: TabNavigator, - Screen, -}; +const MyTab = Tab(); const First = ({ navigation, @@ -150,27 +142,27 @@ function App() { localStorage.setItem(PERSISTENCE_KEY, JSON.stringify(state)) } > - - + - - + {() => ( - - - - + + + + )} - - + + ); } diff --git a/src/createNavigator.tsx b/src/createNavigator.tsx new file mode 100644 index 00000000..3c274581 --- /dev/null +++ b/src/createNavigator.tsx @@ -0,0 +1,19 @@ +import * as React from 'react'; +import { ParamListBase, ScreenProps, TypedNavigator } from './types'; +import Screen from './Screen'; + +export default function createNavigator>( + RawNavigator: N +) { + return function Navigator(): TypedNavigator< + ParamList, + typeof RawNavigator + > { + return { + Navigator: RawNavigator, + Screen: Screen as React.ComponentType< + ScreenProps + >, + }; + }; +} diff --git a/src/index.tsx b/src/index.tsx index 2722a356..706e6615 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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';