【发布时间】:2019-03-03 13:32:50
【问题描述】:
我有一个带有render 和onPress 方法的组件,如下所述...
onCardPressed(event) {
console.log(this.props);
const { data } = this.props;
console.log(event, data);
}
render() {
const { data } = this.props;
return (
<TouchableOpacity
onPress={this.onCardPressed}
>
<Container style={{ elevation: 5 }}>
<SectionTitle>
This is a
{` ${data.city} `}
card
</SectionTitle>
</Container>
</TouchableOpacity>
);
}
在本例中,卡片将正确显示This is a London card,但在onPress方法中this.props返回undefined。
如何访问this.props 对象进行评估?
【问题讨论】:
-
这是函数正确绑定作用域的问题。下面的答案是对的。
标签: react-native