Expo Router UI
一个 Expo Router 子模块,提供无头选项卡组件以创建自定义选项卡布局。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-router/ui 是 expo-router 库的一个子模块,它导出用于构建自定义标签页布局的组件和 hooks,而不是使用 expo-router 提供的默认 React Navigation 导航器。
有关面向原生和 Web 应用的基于文件路由库的更多信息,请参阅 Expo Router 参考文档。
安装
要在你的项目中使用 expo-router/ui,你需要先在项目中安装 expo-router。请按照 Expo Router 安装指南中的说明进行操作:
了解如何在你的项目中安装 Expo Router。
在 app 配置中进行配置
如果你正在使用 默认 模板创建新项目,expo-router 的 配置插件 已经在你的 app 配置中完成设置。
Example app.json with config plugin
{ "expo": { "plugins": ["expo-router"] } }
用法
有关在自定义标签页布局指南中使用 expo-router/ui 的信息:
API
import { Tabs, TabList, TabTrigger, TabSlot } from 'expo-router/ui';
Components
Type: React.Element<ProviderProps<ExpoTabsNavigatorScreenOptions>>
Type: React.Element<TabListProps>
Wrapper component for TabTriggers. TabTriggers within the TabList define the tabs.
Example
<Tabs> <TabSlot /> <TabList> <TabTrigger name="home" href="/" /> </TabList> </Tabs>
Type: React.Element<TabsProps>
Root component for the headless tabs.
See:
useTabsWithChildrenfor a hook version of this component.
Example
<Tabs> <TabSlot /> <TabList> <TabTrigger name="home" href="/" /> </TabList> </Tabs>
booleanForward props to child component and removes the extra <View>. Useful for custom wrappers.
UseTabsOptionsType: React.Element<TabSlotProps>
Renders the current tab.
See:
useTabSlotfor a hook version of this component.
Example
<Tabs> <TabSlot /> <TabList> <TabTrigger name="home" href="/" /> </TabList> </Tabs>
Type: React.Element<TabTriggerProps>
Creates a trigger to navigate to a tab. When used as child of TabList, its
functionality slightly changes since the href prop is required,
and the trigger also defines what routes are present in the Tabs.
When used outside of TabList, this component no longer requires an href.
Example
<Tabs> <TabSlot /> <TabList> <TabTrigger name="home" href="/" /> </TabList> </Tabs>
booleanForward props to child component. Useful for custom wrappers.
stringName of tab. When used within a TabList this sets the name of the tab.
Otherwise, this references the name.
unionResets the route when switching to a tab.
Acceptable values are: SwitchToOptions[reset] | 'onLongPress'
Type: React.Element<TabSlotProps>
Returns a ReactElement of the current tab.
Example
function MyTabSlot() { const slot = useTabSlot(); return slot; }
Hooks
| Parameter | Type |
|---|---|
| namedParameters(optional) | TabSlotProps |
Returns a ReactElement of the current tab.
ElementExample
function MyTabSlot() { const slot = useTabSlot(); return slot; }
| Parameter | Type |
|---|---|
| options | UseTabsWithChildrenOptions |
Hook version of Tabs. The returned NavigationContent component
should be rendered. Using the hook requires using the <TabList />
and <TabTrigger /> components exported from Expo Router.
The useTabsWithTriggers() hook can be used for custom components.
See:
Tabsfor the component version of this hook.
Example
export function MyTabs({ children }) { const { NavigationContent } = useTabsWithChildren({ children }) return <NavigationContent /> }
| Parameter | Type |
|---|---|
| options | UseTabsWithTriggersOptions |
Alternative hook version of Tabs that uses explicit triggers
instead of children.
TabsContextValueSee:
Tabsfor the component version of this hook.
Example
export function MyTabs({ children }) { const { NavigationContent } = useTabsWithChildren({ triggers: [] }) return <NavigationContent /> }
| Parameter | Type |
|---|---|
| options | TabTriggerProps |
Utility hook creating custom TabTrigger.
UseTabTriggerResultTypes
Type: NavigationProp<ParamList, RouteName, NavigatorID, TabNavigationState<ParamListBase>, ExpoTabsScreenOptions, TabNavigationEventMap>
Literal Type: union
Acceptable values are: DefaultNavigatorOptions<ParamListBase, string | undefined, TabNavigationState<ParamListBase>, ExpoTabsScreenOptions, TabNavigationEventMap, ExpoTabsNavigationProp<ParamListBase>> | Omit<TabRouterOptions, 'initialRouteName'> | ExpoTabsNavigatorScreenOptions
| Property | Type | Description |
|---|---|---|
| detachInactiveScreens(optional) | boolean | - |
| freezeOnBlur(optional) | boolean | - |
| lazy(optional) | boolean | - |
| unmountOnBlur(optional) | boolean | - |
Literal Type: string
Acceptable values are: 'always' | 'onFocus' | 'never'
Type: Pick<BottomTabNavigationOptions, 'title' | 'lazy' | 'freezeOnBlur'> extended by:
| Property | Type | Description |
|---|---|---|
| action | NavigationAction | - |
| params(optional) | object | - |
| title | string | - |
Options for switchTab function.
| Property | Type | Description |
|---|---|---|
| reset(optional) | ExpoTabsResetValue | Navigate and reset the history. |
| Property | Type | Description |
|---|---|---|
| tabLongPress | {
data: undefined
} | Event which fires on long press on the tab in the tab bar. |
| tabPress | {
canPreventDefault: true,
data: undefined
} | Event which fires on tapping on the tab in the tab bar. |
Type: ReturnType<useNavigationBuilder>
The React Navigation custom navigator.
See:
useNavigationBuilderhook from React Navigation for more information.
Options provided to the UseTabSlotOptions.
| Property | Type | Description |
|---|---|---|
| detachInactiveScreens | boolean | Should the screen be unloaded when inactive. |
| index | number | Index of screen. |
| isFocused | boolean | Whether the screen is focused. |
| loaded | boolean | Whether the screen has been loaded. |
| Property | Type | Description |
|---|---|---|
| href | Href | - |
| name | string | - |
Type: extended by:
| Property | Type | Description |
|---|---|---|
| isFocused | boolean | - |
| resolvedHref | string | - |
| route | [number] | - |
Options to provide to the Tab Router.
Type: Omit<DefaultNavigatorOptions<ParamListBase, any, TabNavigationState<any>, ExpoTabsScreenOptions, TabNavigationEventMap, any>, 'children'> extended by:
| Property | Type | Description |
|---|---|---|
| backBehavior(optional) | TabRouterOptions[backBehavior] | - |
Type: PropsWithChildren<UseTabsOptions>
Type: UseTabsOptions extended by:
| Property | Type | Description |
|---|---|---|
| triggers | ScreenTrigger[] | - |
| Property | Type | Description |
|---|---|---|
| getTrigger | (name: string) => Trigger | undefined | - |
| switchTab | (name: string, options: SwitchToOptions) => void | - |
| trigger(optional) | Trigger | - |
| triggerProps | TriggerProps | - |