如何解决route.params在react原生屏幕中未定义的问题typescript

mxg2im7a  于 2022-11-17  发布在  React
关注(0)|答案(3)|浏览(137)

下面是我从API获取国家I地区名称并使用routes将其显示在屏幕上的情况。现在,我想将item.country显示为headerTitle。但是,对于route.params.,我得到了一个undefined错误。如何修复此错误?

位置数据屏幕.tsx

function LocationDataScreen({ route, navigation }) {
  const { item } = navigation.state.params || {};  
  const countryName = item.country;
  return (
    <View style={styles.container}>
      <Text>{countryName}</Text>
      <Text>{item.cases}</Text>
    </View>
  );
}

项目视图.tsx

const ItemView = ({ item }) => {
    return (
      <RectButton
        onPress={() =>
          navigate("LocationDataScreen", { item: item, name: item.country })
        }
      >
        <Text style={[styles.itemStyle, { textTransform: "capitalize" }]}>
          {item.id}
          {item.country.toUpperCase()}
        </Text>
      </RectButton>
    );
  };

应用程序tsx

<AppStack.Navigator initialRouteName="SearchScreen">
  <AppStack.Screen name="SearchScreen" component={SearchScreen} />
  <AppStack.Screen
          name="LocationDataScreen"
          component={LocationDataScreen}
          options={({ route }) => ({
            headerLargeTitle: true,
            title: route.params.item,
          })}
        />
bxgwgixi

bxgwgixi1#

你应该像下面这样做

options={({route}) => ({
            headerLargeTitle: true,
            title: route.params.name,
          })}

您必须像下面这样传递名称

navigation.navigate("LocationDataScreen",{name:item.name});

请确保您具有如下所示的设置,以便访问参数

import { createStackNavigator } from '@react-navigation/stack';

type RootStackParamList = {
  SearchScreen: undefined;
  LocationDataScreen: { name: string };
};

const AppStack = createStackNavigator<RootStackParamList>();
hwazgwia

hwazgwia2#

问题很简单,您以错误的方式将参数传递给LocationDataScreen.tsx
请使用以下命令:

项目视图

const ItemView = ({ item }) => {
    return (
      <RectButton
        onPress={() =>
          navigate("LocationDataScreen", { ...item })
        }
      >
        <Text style={[styles.itemStyle, { textTransform: "capitalize" }]}>
          {item.id}
          {item.country.toUpperCase()}
        </Text>
      </RectButton>
    );
  };

位置数据屏幕.tsx

function LocationDataScreen({ route, navigation }) {
  const { country, cases } = route.params

  return (
    <View style={styles.container}>
      <Text>{country}</Text>
      <Text>{cases}</Text>
    </View>
  );
}

我在这里假设您的item在这一行const ItemView = ({ item }) => {看起来像这样。

item: {
   country: 'Italy',
   cases: 10
}

如果你可以添加你的api响应这里。让我知道在评论一旦你添加。也分享链接到相同的评论。

42fyovps

42fyovps3#

根据React导航文档,将类型设置为;

import { NativeStackScreenProps } from "@react-navigation/native-stack";

export type StackParamList = {
  screen1: undefined;
  screen2: {propkey: proptype, ...}; // this object defines the params and their types passed to the screen
};

export type ScreenProps = NativeStackScreenProps<
  StackParamList,
  "screen1"
>;

然后在你的屏幕上;

import {ScreenProps} from 'path_to_above_file' // or put the above defined type in the same screen

然后为屏幕定义界面;

interface CurrentScreenProps {
  navigation: DashProps["navigation"];
  route: DashProps["route"];
}

在这之后,当使用route.params.key时,它会给出警告:last route.params可能是一个空对象。

interface CurrentScreenProps {
   ...,
   route: {
     key1: key1type, // e,g, name: string;
     //...other keys and their types

   }

}

相关问题