我是反应原生的新手。我的总体目标是将passedThis
从myfunction1
传递到chPSCallback
。因此,我passedThis
值变成了公共值,以便我可以在function2
中使用它。这是我的代码:
var passedThisGlob;
var start1 = {
myfunction1: function(ID, passedThis) {
var callbacks = {
chPS: this.chPSCallback,
};
passedThisGlob = passedThis;
Mylib.start(ID, callbacks);
},
chPSCallback: function(positionState) {
passedThis.props.navigation.navigate('test', { user: passedThis.state.user });
alert('Test works!);
},
}
更新:我发现,chPSCallback
首先执行。
我需要使用 passedThis
在chPSCallback
函数中进行导航,以便我需要在其中passedThis
。你能帮我怎么做吗?全局变量解决方案是个好主意吗?
试试这个
var start = {
myfunction1: function(ID, passedThis) {
var callbacks = {
chPS: function(){this.chPSCallback()},
};
this._passedThis = passedThis;
Mylib.start(ID, callbacks);
},
chPSCallback: function(positionState) {
this._passedThis.props.navigation.navigate('test', { user: this._passedThis.state.user });
alert('Test works!);
},
}