【发布时间】:2022-01-21 17:55:32
【问题描述】:
我正在尝试使用 React Native 中的 Map 函数从 JSON 数据生成卡片。
我希望能够通过单击此卡片导航到另一个页面。
这是我正在尝试的解决方案:
function display() {
return restaurant.map((item) => {
return(
<TouchableHighlight onPress={() => this.props.navigation.navigate('Restaurant')}>
<View style={styles.card}>
<View style={styles.cardHeadText}>
<Text style={styles.title}>
{ item.name }
</Text>
<Text>
{ item.type }
</Text>
</View>
</View>
</TouchableHighlight>
);
});
}
class RestaurantCard extends Component {
render() {
return (
<View style={styles.container}>
{display()}
</View>
);
}
}
但我收到以下错误:
未定义不是一个对象(评估'_this.props.navigation')
我做错了什么?
【问题讨论】:
-
在这里进行真正的盲刺,但在
function display()下方输入const that = this之类的内容,然后将您的onPress 更改为that.props.navi....