简体   繁体   English

如何在反应导航 5 和 TypeScript 中键入 Tab Navigator

[英]How to type Tab Navigator in react navigation 5 and TypeScript

import React from 'react';
import { Text, TouchableOpacity, View } from 'react-native';
import {
  BottomTabNavigationProp,
  createBottomTabNavigator,
} from '@react-navigation/bottom-tabs';
import { TabNavigationState } from '@react-navigation/native';


export type RootParamList = {
  Feed: undefined;
  Search: undefined;
};

export enum Screens {
  FEED = 'Feed',
  SEARCH = 'Search',
}
const Tabs = createBottomTabNavigator<RootParamList>();

export interface TabbarProps {
  navigation: BottomTabNavigationProp<RootParamList, Screens>;
  state: TabNavigationState<RootParamList>;
}

const TabBar = (props: TabbarProps) => {
  const handleTabPress = ({
    name,
    isCurrentTab,
  }: {
    name: keyof RootParamList;
    isCurrentTab: boolean;
  }) => {
    if (!isCurrentTab) {
      props.navigation.navigate(name);
    }
  };
  return (
    <View>
      {props.state.routes.map((route, index) => {
        const buttonText = route.name;
        const isCurrentTab = props.state.index === index;
        return (
          <TouchableOpacity
            key={index}
            onPress={() => handleTabPress({ name: route.name, isCurrentTab })}>
            <Text>{buttonText}</Text>
          </TouchableOpacity>
        );
      })}
    </View>
  );
};

const Nav = () => (
  <Tabs.Navigator
    tabBar={({ navigation, state }) => (
      <TabBar navigation={navigation} state={state} />
    )}>
    <Tabs.Screen name={Screens.FEED} component={View} />
    <Tabs.Screen name={Screens.SEARCH} component={View} />
  </Tabs.Navigator>
);

export default Nav;

There is an issue with <Tab.Navigator /> and the tabbar prop. <Tab.Navigator />tabbar属性存在问题。 I can't seem to type it correctly for my specific navigation and state props.我似乎无法为我的特定navigationstate道具正确键入它。

在此处输入图像描述

Late to answer, even I was stuck with the same issue, following changes helped me,迟到了,即使我也遇到了同样的问题,以下变化帮助了我,

Please note I have used type instead of interface, interface will also work.请注意,我使用了类型而不是接口,接口也可以使用。

In your TabBar,在您的标签栏中,

Instead of代替

export interface {
  navigation: BottomTabNavigationProp<RootParamList, Screens>;
  state: TabNavigationState<RootParamList>;
}

Use利用

type TabbarProps = {
  navigation: NavigationHelpers<ParamListBase, BottomTabNavigationEventMap>;
  state: TabNavigationState<ParamListBase>;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当它可以接受参数时,如何使用打字稿键入反应导航导航器 - How do you type a react-navigation navigator with typescript when it can accept params 如何在 reactnavigation typescript react native 中结合标签和堆栈导航道具 - How to combine tab and stack navigation props in reactnavigation typescript react native 您是否能够在 React Navigation Tab Navigator 中以编程方式生成屏幕 - Are you able to programmatically generate screens inside React Navigation Tab Navigator React Navigation 自定义选项卡 Typescript 参数 - React Navigation Custom Tab Typescript Params 反应导航:如何在打字稿中使用 NavigationStackScreenComponent 类型传递 redux 道具 - react navigation: how to pass redux props with NavigationStackScreenComponent type in typescript typescript 中的路线(反应导航)类型 - route (react-navigation) type in typescript 路由器位置导航器 typescript,反应 - Router location navigator typescript, react React Navigation TypeScript:类型…的参数不能分配给类型&#39;BottomTabNavigatorConfig&#39;的参数 - React Navigation TypeScript: Argument of type … is not assignable to parameter of type 'BottomTabNavigatorConfig' 类型中缺少 TypeScript 属性“导航”,但类型“道具”React Native 中需要 - TypeScript Property 'navigation' is missing in type but required in type 'Props' React Native 如何在 react-navigation 库中键入导航? - How to type navigation in react-navigation library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM