Reactjs 无法在react native中绑定函数
我已经尝试了我所知道的所有可能的解决办法。我尝试过其他几种解决类似问题的方法,但我不明白这是怎么回事。我试图从ListView renderRow方法内部调用函数。我现在可以调用该函数,但无法传入所需的选定行数据。这就是我得到的: 我需要选定的行数据。这是代码Reactjs 无法在react native中绑定函数,reactjs,react-native,ecmascript-6,Reactjs,React Native,Ecmascript 6,我已经尝试了我所知道的所有可能的解决办法。我尝试过其他几种解决类似问题的方法,但我不明白这是怎么回事。我试图从ListView renderRow方法内部调用函数。我现在可以调用该函数,但无法传入所需的选定行数据。这就是我得到的: 我需要选定的行数据。这是代码 constructor(props) { super(props); this.onSubmitTrip = this.onSubmitTrip.bind(this); this
constructor(props) {
super(props);
this.onSubmitTrip = this.onSubmitTrip.bind(this);
this.onSelectTrip = this.onSelectTrip.bind(this);
this.renderRow = this.renderRow.bind(this);
this.state = {
dataSource: ds,
showSubmitTripBtn: false
}
}
onSelectTrip(rowData) {
selectedTrip = rowData;
}
renderRow(rowData) {
return (
<TouchableHighlight onPress={this.onSelectTrip} underlayColor='#dddddd'>
<View style={styles.row}>
<View style={{ paddingLeft: 5 }}>
<View>
<Text style={{ fontWeight: "bold", fontSize: 24 }}>{rowData.tripName}</Text>
</View>
</View>
</View>
</TouchableHighlight>
)
}
构造函数(道具){
超级(道具);
this.onSubmitTrip=this.onSubmitTrip.bind(this);
this.onSelectTrip=this.onSelectTrip.bind(this);
this.renderRow=this.renderRow.bind(this);
此.state={
数据源:ds,
showSubmitTripBtn:false
}
}
onSelectTrip(行数据){
selectedTrip=rowData;
}
renderRow(行数据){
返回(
{rowData.tripName}
)
}
使用ES6 lambda而不是显式绑定类构造函数中的所有方法来提供词法绑定怎么样。我认为这应该解决你的问题:
constructor(props) {
super(props);
this.onSubmitTrip = this.onSubmitTrip.bind(this);
this.renderRow = this.renderRow.bind(this);
this.state = {
dataSource: ds,
showSubmitTripBtn: false
}
}
onSelectTrip = (rowData) => {
selectedTrip = rowData;
}
renderRow(rowData) {
return (
<TouchableHighlight onPress={this.onSelectTrip(rowData)} underlayColor='#dddddd'>
<View style={styles.row}>
<View style={{ paddingLeft: 5 }}>
<View>
<Text style={{ fontWeight: "bold", fontSize: 24 }}>{rowData.tripName}</Text>
</View>
</View>
</View>
</TouchableHighlight>
)
}
构造函数(道具){
超级(道具);
this.onSubmitTrip=this.onSubmitTrip.bind(this);
this.renderRow=this.renderRow.bind(this);
此.state={
数据源:ds,
showSubmitTripBtn:false
}
}
onSelectTrip=(行数据)=>{
selectedTrip=rowData;
}
renderRow(行数据){
返回(
{rowData.tripName}
)
}
好的,我可以看到,当按下TouchableHighlight时,您实际上并没有在SelectTrip上传递您的行数据
// Change this
<TouchableHighlight
onPress={this.onSelectTrip}
underlayColor='#dddddd'
>
// To this
<TouchableHighlight
onPress={() => this.onSelectTrip(rowData)}
underlayColor='#dddddd'
>
它似乎正在页面加载时触发函数。除了代码下面的一堆红线,我得到了TypeError:无法读取未定义(…)的属性“setState”!!!以上代码是否完整?我没有看到任何
setState
no,我将用它创建plunker。我不确定您需要什么。请尝试更改的匿名函数。在onSelectTrip
方法中,每个都使用lambda。它允许加载视图而不会出现问题。但函数没有在按下“我现在可以调用函数,但我无法传入所需的选定行数据”时触发。我看不到您在哪里调用该方法。传递数据非常简单,如this.renderRow(theData)
。
// Change this
_.each(rowData.routeLocations, function(item) {
// To this
_.each(rowData.routeLocations, (item) => {