我有一个 React 组件,例如:
function callback(params){..
// I need to use this.setstate but this callback function is called
// from other component. How do I bind value of this here
// 'this' of RespProperties
...
}
class RespProperties extends Component { ..
...
}
此回调函数是从其他组件调用的。如何在此处绑定(bind)“this”的值,以便它可以使用该组件的状态?
最佳答案
我不太明白这个问题。 我不知道这是否是你的意思,但如果你想保存'this'临时变量,那么只需创建一个全局数组或单个变量来存储'this'。
var thisTemp;
function callback(params){..
// use variable here
thisTemp.blah();
...
}
class RespProperties extends Component { ..
//Assign this to thisTemp
thisTemp = this;
...
}
关于javascript - 如何将 'this' 绑定(bind)到 React 类之外的函数(来自其他组件的回调)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36372509/