【发布时间】:2021-07-27 01:03:43
【问题描述】:
我在下面有这个,我从 API 获取国家/地区名称,并使用 routes 在屏幕上显示它们。现在,我想将item.country 显示为headerTitle。但是,我收到了 route.params. 的 undefined 错误我该如何解决这个问题?
LocationDataScreen.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>
);
}
ItemView.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>
);
};
App.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,
})}
/>
【问题讨论】:
标签: typescript react-native react-router navigation stack-navigator