React-Native 无法读取未定义的 属性 'bind'
React-Native Cannot read property 'bind' of undefined
两个问题,如果我像这样绑定我的函数:
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>
);
});
}
我的错误是'无法读取未定义的属性 '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>
);
});
}
有人可以帮助我吗?谢谢!
这是因为你忘记绑定this
和map回调函数,回调函数里面的this
不是指react class上下文,这里:
tabTag.map(function(item, id){ .... })
tabTag.map((item, id) => { .... })
现在用第一种或第二种方法写正文,两者都行。
两个问题,如果我像这样绑定我的函数:
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>
);
});
}
我的错误是'无法读取未定义的属性 '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>
);
});
}
有人可以帮助我吗?谢谢!
这是因为你忘记绑定this
和map回调函数,回调函数里面的this
不是指react class上下文,这里:
tabTag.map(function(item, id){ .... })
tabTag.map((item, id) => { .... })
现在用第一种或第二种方法写正文,两者都行。