Skip to content

Instantly share code, notes, and snippets.

@chamatt
Last active November 6, 2022 23:03
Show Gist options
  • Save chamatt/17c2aa78b78968ca31d8fe2204aaef6d to your computer and use it in GitHub Desktop.
Save chamatt/17c2aa78b78968ca31d8fe2204aaef6d to your computer and use it in GitHub Desktop.
Expo-router utility to set options
import { WithNavigation } from 'WithNavigation'
const HomeScreen = () => {
return (
<div> Some Content </div>
)
}
export default WithNavigation(HomeScreen, {
type: 'stack',
options: {
title: 'Home Screen',
},
})
export type RouteTypes = {
home: undefined
postDetail: { id: string }
postList: undefined
login: undefined
signUp: undefined
}
type ScreenOptionsProps = {
stack: Parameters<typeof Stack['Screen']>['0']
tabs: Parameters<typeof Tabs['Screen']>['0']
}
type WithNavigationProps =
| [
ComponentType<NativeStackScreenProps<RouteTypes, keyof RouteTypes>>,
{ type: 'stack' } & ScreenOptionsProps['stack']
]
| [
ComponentType<BottomTabScreenProps<RouteTypes, keyof RouteTypes>>,
{ type: 'tabs' } & ScreenOptionsProps['tabs']
]
export const WithNavigation = (...args: WithNavigationProps) => {
const [Component, navigationOptions] = args
const { type, ...navigationProps } = navigationOptions
const NavComponent = () => {
switch (type) {
case 'stack':
return (
<Stack.Screen {...(navigationProps as ScreenOptionsProps['stack'])} />
)
case 'tabs':
return (
<Tabs.Screen {...(navigationProps as ScreenOptionsProps['tabs'])} />
)
default:
return null
}
}
return (props: any) => {
return (
<>
<NavComponent />
<Component {...props} />
</>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment