【发布时间】:2015-08-06 20:44:07
【问题描述】:
我知道有类似的问题,但没有一个可以帮助我解决我的问题。
所以这是我的问题。
我正在使用 react native 并使用通量调度程序。我的应用调度程序的调度和注册工作正常。我的问题是,当我想更改/设置调度寄存器函数中的状态时,我总是收到 this.setState() 不是函数的错误消息。当然我当时认为这一定是一个绑定问题(用 es6 编写),所以我尝试了各种绑定“this”但我仍然无法让它工作。有人知道为什么吗?
下面是那段不起作用的代码:
testDispatcher() {
AppDispatcher.register( (action) => {
if ( action.action === TEST_ACTION ) {
// I tried setting state inside here
this.setState({
view: action.view
}).bind(this); // with or without this bind doesn't make a difference
// I also tried having a function outside of this function where I set the state.. this doesn't work either.
//this.updateView('home').bind(this);
console.log('dispatch register');
}
});
}
我还尝试在我的注册函数中控制台记录“this”,并且“this”确实返回了我的应用程序类。
【问题讨论】:
标签: javascript reactjs ecmascript-6 flux