我是 reactjs 的初学者,我正在尝试在 api 响应中设置材料,但是“this”变得未定义,所以我不能调用 this.setMaterials(materials) 为什么这在 getMaterials() 函数中变得未定义?
constructor() {
super();
this.obj = {
materials: []
};
}
getMaterials(string) {
var options = {
url: 'materials',
method: 'get',
};
http.makeRequest(options).then(res => {
console.log(this)// undefined
this.setMaterials(res); //try to set materials in the obj variable
});
}
setMaterials(materials) {
this.obj.materials = materials;
}
render(){
return(
<IntegrationDownshift getItems={this.getMaterials.bind(this)} />
)
}