在组件ContactList
中,我已经使用地图渲染了一些项目。每个项目都包含一个缩略图。单击缩略图时,我想导航到一个名为UserDetailsScreen
的新屏幕。在导航时,我还想将有关单击缩略图的特定用户的数据传输到下一个屏幕。
模态无法正常工作,因为如果我在地图中使用了多个模态,则会打开多个模态。所以我正在尝试使用反应导航。
ContactList.tsx:
export const ContactList: React.FunctionComponent<UserProps> = ({
data,
onDeleteContact,
}) => {
const [isUserVisible, setIsUserVisible] = useState(false);
//const [visibleUser, setVisibleUser] = useState<any>();
const navigation = useNavigation();
return (
<View style={styles.users}>
{data.users.nodes[0].userRelations.map(
(item: { relatedUser: RelatedUser; id: number }) => {
const numberOfFriends = item.relatedUser.userRelations.length;
const numberPlate = 'WHV AB 123';
return (
<View style={styles.item} key={item.id}>
{/* <TouchableOpacity onPress={() => setIsUserVisible(true)}> */}
<TouchableOpacity
onPress={() =>
navigation.navigate('UserDetailsScreen', {
firstName: item.relatedUser.firstName,
rating: item.relatedUser.rating,
numberOfFriends: numberOfFriends,
onDeleteContact: onDeleteContact,
isUserVisible: isUserVisible,
setIsUserVisible: setIsUserVisible,
numberPlate: numberPlate,
navigation: navigation,
})
}>
<Thumbnail
}}></Thumbnail>
</TouchableOpacity>
<View style={styles.nameNumber}>
<Text style={styles.userName}>{userName}</Text>
</View>
{/* <UserDetails
firstName={item.relatedUser.firstName}
rating={item.relatedUser.rating}
numberOfFriends={numberOfFriends}
onDeleteContact={onDeleteContact}
isUserVisible={isUserVisible}
setIsUserVisible={setIsUserVisible}
numberPlate={numberPlate}>
</UserDetails> */}
</View>
);
},
)}
</View>
);
};
UserDetailsScreen:
type UserProps = {
//data: UsersQueryHookResult,
firstName: string;
rating: number;
numberOfFriends: number;
numberPlate: string;
onDeleteContact: (id: number) => void;
navigation: any;
};
export const UserDetailsScreen: React.FunctionComponent<UserProps> = ({
firstName,
rating,
numberOfFriends,
numberPlate,
onDeleteContact,
navigation,
// isUserVisible,
// setIsUserVisible,
}) => {
//const navigation = useNavigation();
const fName = navigation.getParam('firstName')
return (
// <Modal visible={isUserVisible}>
<View style={styles.container}>
<View>
<TouchableOpacity
style={styles.cross}
//onPress={() => setIsUserVisible(false)}>
onPress={() => navigation.navigate('Whitelist')}>
<Thumbnail></Thumbnail>
</TouchableOpacity>
</View>
<View style={styles.searchLocationContainer}>
<UserInfoContainer
firstName={firstName}
rating={rating}
numberPlate={numberPlate}
numberOfFriends={numberOfFriends}></UserInfoContainer>
</View>
</View>
// </Modal>
);
};
此外,当我从该屏幕上单击缩略图时,我应该返回上一个屏幕,以便现在可以单击另一个对象。
目前,我不断收到错误消息,指出navigation.getParam
未定义。我该如何解决?
我认为我需要通过route
道具,但是我不确定如何使用它们以及是否应该在两个屏幕或一个屏幕中通过
答案 0 :(得分:1)
从路线道具获取数据
喜欢
thisArg
Array#filter
如果您可以导航到type UserProps = {
//data: UsersQueryHookResult,
firstName: string;
rating: number;
numberOfFriends: number;
numberPlate: string;
onDeleteContact: (id: number) => void;
navigation: any;
route: any;
};
export const UserDetailsScreen: React.FunctionComponent<UserProps> = ({
firstName,
rating,
numberOfFriends,
numberPlate,
onDeleteContact,
navigation,
route,
// isUserVisible,
// setIsUserVisible,
})....
,则无需通过任何路线。因为导航及其属性是通过route.params.firstName
答案 1 :(得分:1)
您将参数传递给UserDetailsScreen的方法是正确的。
在以前版本的react-navigation(1.x至4.x)中,您可以在“ navigation.state.params”中获取参数。
在react-navigation 5中,他们更改了其实现。现在,您可以将参数传递到“ route.params”对象中的屏幕或组件。您可以在下面查看代码以供参考。
type UserProps = {
route: any,
navigation: any,
};
export const UserDetailsScreen: React.FunctionComponent<UserProps> = ({
route,navigation
}) => {
const {
firstname,
rating,
numberOfFriends,
numberPlate,
onDeleteContact,
} = route.params;
return (
<View style={styles.container}>
<View>
<TouchableOpacity
style={styles.cross}
//onPress={() => setIsUserVisible(false)}>
onPress={() => navigation.navigate('Whitelist')}>
<Thumbnail />
</TouchableOpacity>
</View>
<View style={styles.searchLocationContainer}>
<UserInfoContainer
firstName={firstName}
rating={rating}
numberPlate={numberPlate}
numberOfFriends={numberOfFriends}
/>
</View>
</View>
);
};