0 javascript flux ecmascript-6 reactjs
我知道也有类似的问题,但是没有一个问题能帮助我解决问题。
所以这是我的问题。
我正在与本机反应并使用通量分配器。我的应用程序分派器的分派和注册工作正常。我的问题是,当我想更改/设置调度寄存器函数中的状态时,总是收到错误消息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');
}
});
}
Run Code Online (Sandbox Code Playgroud)
我还尝试在注册函数中管理日志“ this”,并且“ this”确实返回了我的应用程序类。
将=>结合this到的范围testDispatcher()。那可能不是您想要的。在这种情况下,我认为您应该删除该=>表示法,而只是一个常规的匿名函数。
另外,this.setState(...args...).bind(this)是完全错误的。1)该this.部分表示.bind(this)冗余。2)绑定的语法类似于:foo.setState.bind(notFoo, ...args...)。
| 归档时间: |
|
| 查看次数: |
5798 次 |
| 最近记录: |