I've rewrite a component from createClass to class definition to fit eslint-react-native linting, then I found I cannot bind function to element in Array as it used to. The former code looks like this:
createClass({
render() {
return (
<ListView style={styles.listView}
dataSource={this.state.data}
renderRow={this._renderTopic}
/>
)
},
_renderTopic(topic) {
return (
<TouchableHighlight onDelayColor="#dddddd"
onPress={() => this._jumpTo(topic.id) }
>
)
},
_jumpTo(id) {
this.props.navigator.push({
name: 'Topic page',
component: Topic,
passProps: {
topicId: id,
},
});
}
})
When I change it to class definition:
{
render() {
return (
<ListView style={styles.listView}
dataSource={this.state.data}
renderRow={this._renderTopic}
/>
)
}
_renderTopic(topic) {
return (
<TouchableHighlight onDelayColor="#dddddd"
onPress={() => this._jumpTo(topic.id) }
>
)
}
_jumpTo(id) {
this.props.navigator.push({
name: 'Topic page',
component: Topic,
passProps: {
topicId: id,
},
});
}
}
It does not work again. And give an error while press the Touchable: this2._jumpTo is not a function
So I changed it again:
{
render() {
return (
<ListView style={styles.listView}
dataSource={this.state.data}
renderRow={this._renderTopic}
/>
)
}
_renderTopic(topic) {
const nav = this.props.navigator
let jumpTo = function(id) {
nav.push({
name: 'Topic page',
component: Topic,
passProps: {
topicId: topic.id,
},
});
};
return (
<TouchableHighlight onDelayColor="#dddddd"
onPress={jumpTo}
>
)
}
}
This would through error immediately: possible unhandled promise rejection cannot read property _currentElement of null. (Which is strange here, the jumpTo function should be lazy, right?)
So what's the right way to bind an dynamic function to item in ListView?