【发布时间】:2019-06-08 16:11:26
【问题描述】:
我正在开发一个用于学习目的的 React Native 应用程序。现在我正在使用 TouchableWithoutFeedback 组件来响应用户交互。但是我遇到了一个错误。
这是我的代码:
class Events extends React.Component {
static navigationOptions = {
title: "Events"
};
constructor(props) {
super(props);
this.state = {
data: [
{
id: 1,
name: "Name 1",
image_url: "https://www.vkguy.co.uk/images/slideshow/05.jpg"
},
{
id: 2,
name: "Name 2",
image_url: "https://www.vkguy.co.uk/images/slideshow/05.jpg"
},
{
id: 3,
name: "Name 3",
image_url: "https://www.vkguy.co.uk/images/slideshow/05.jpg"
}
]
};
}
_handleLoadMore() {}
renderItem(item) {
return (
<TouchableWithoutFeedback onPress={() => {
}}>
<View>
<Card>
<CardItem cardBody>
<Image
source={{ uri: item.image_url }}
style={{ height: 200, width: null, flex: 1 }}
/>
</CardItem>
<CardItem>
<Left>
<Button transparent>
<Icon active name="thumbs-up" />
<Text>12 Likes</Text>
</Button>
</Left>
<Body>
<Button transparent>
<Icon active name="chatbubbles" />
<Text>4 Comments</Text>
</Button>
</Body>
<Right>
<Text>11h ago</Text>
</Right>
</CardItem>
</Card>
</View>
</TouchableWithoutFeedback>
);
}
render() {
return (
<View style={{ flex: 1, width: "100%" }}>
<FlatList
data={this.state.data}
keyExtractor={item => item.id.toString()}
renderItem={({ item }) => this.renderItem(item)}
onEndReached={this._handleLoadMore}
/>
</View>
);
}
}
export default Events;
当我点击视图时,我得到了这个错误。
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
我的代码有什么问题,我该如何解决?
【问题讨论】:
-
你的
renderItem函数绑定了吗?在提供的代码中看不到这一点。尝试通过在构造函数中添加this.renderItem=this.renderItem.bind(this)来绑定函数。或者简单地将函数转换为粗箭头函数()=>{}。 -
嗨,Nishant,我在构造函数中添加了绑定。但还是不行。
标签: react-native touchablewithoutfeedback