Eli*_*ert 2 native reactjs react-native
两个问题,如果我这样绑定我的函数:
deleteTag = (id) => {
console.log(id);
id = 0;
tabTag.splice(id, 1);
--tabSize;
}
componentTag() {
return tabTag.map(function(item, id){
return(
<View key={id} style={styles.componentView}>
<Icon name="ios-reorder"></Icon>
<Text>{item.name}</Text>
<Slider style={styles.sliderBar} maximumValue={3} step={1} />
<TouchableHighlight onPress={() => this.deleteTag.bind(this)}>
<Icon name="close-circle"/>
</TouchableHighlight>
</View>
);
});
}
Run Code Online (Sandbox Code Playgroud)
我的错误是'无法读取未定义的属性'bind'
其他
如果我将函数绑定到构造函数中,则不会发生任何事情
constructor(props) {
this.deleteTag = this.deleteTag.bind(this);
}
deleteTag = (id) => {
console.log(id);
id = 0;
tabTag.splice(id, 1);
--tabSize;
}
componentTag() {
return tabTag.map(function(item, id){
return(
<View key={id} style={styles.componentView}>
<Icon name="ios-reorder"></Icon>
<Text>{item.name}</Text>
<Slider style={styles.sliderBar} maximumValue={3} step={1} />
<TouchableHighlight onPress={this.deleteTag}>
<Icon name="close-circle"/>
</TouchableHighlight>
</View>
);
});
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我吗?谢谢 !
这是因为您忘记了this与地图回调函数绑定,并且this内部回调函数未引用React类上下文,此处:
tabTag.map(function(item, id){ .... })
使用箭头功能:
tabTag.map((item, id) => { .... })
Run Code Online (Sandbox Code Playgroud)
现在,使用您的第一种或第二种方法来编写正文,两者都将起作用。
| 归档时间: |
|
| 查看次数: |
3341 次 |
| 最近记录: |