我在使用ListItems(react-native-element)和Flatlist构建的“ NerdList”屏幕上有一个配置文件列表。当您从列表中单击一个配置文件时,该应用程序应导航到“配置文件”屏幕并传递测试参数。NerdList屏幕:
import React from "react";
import { withNavigation } from '@react-navigation/compat';
import { StyleSheet, FlatList} from "react-native";
import { ListItem } from "react-native-elements";
import nerdProfiles from '../constants/nerdProfiles';
import { Block, Text } from 'galio-framework';
import argonTheme from "../constants/Theme";
import { TouchableOpacity } from "react-native-gesture-handler";
class NerdList extends React.Component {
renderItem = ({item}) => (
<TouchableOpacity
onPress={() => this.props.navigation.navigate('Profile', {test: 'Hello'})}
>
<ListItem
title={
<Block>
<Text style={styles.names}>
{item.name}
</Text>
</Block>
}
subtitle={
<Block>
<Text style={styles.descriptions}>
{item.shortDescription}
</Text>
</Block>
}
leftAvatar={{ source: { uri: item.image } }}
bottomDivider
chevron
/>
</TouchableOpacity>
);
render() {
return (
<FlatList
data={nerdProfiles.bios}
renderItem={this.renderItem}
keyExtractor={item => item.id}
/>
);
};
};
export default withNavigation(NerdList);
仅导航有效,但是当我尝试传递参数时,我收到TypeError:undefined不是对象(评估'_ref3.route')。
这是我用来在“配置文件”屏幕上接收路线的代码:
const {test} = route.params;
在个人资料屏幕更改中
const {test} = route.params;
至const { params } = this.props.navigation.state;
const test= params ? params.test: null;
希望这会有所帮助!