react native Flatlist navigation

徘徊边缘 提交于 2019-12-06 20:24:31

Inside you renderItem method, you can manage what happen when the user press one item of your FlatList:

renderItem({ item }) {
    <TouchableOpacity onPress={() => { this.props.navigator.push({id: 'employeeEdit'})}} >
        <ListEmployee employee={item} navigation={this.props.navigation} />
    </TouchableOpacity>
}

Hope it help you!

This is one ES6 common pitfall. Don't worry my friend, you only have to learn it once to avoid them all over again.

Long story short, when you declare a method inside React Component, make it arrow function

So, change from this.

renderItem({ item }) {

to this

renderItem = ({ item }) => {

That should solve your problem, for some inconvenient reason, you can only access "this" if you declare your method as an arrow function, but not with normal declaration.

In your case, since renderItem is not an arrow function, "this" is not referred to the react component, therefore "this.props" is likely to be undefined, that is why it gave you this error Cannot read property 'navigation' of undefined since

this.props.navigation = (undefined).navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!