繁体   English   中英

this.setState不是函数,本机反应

[英]this.setState is not a function, 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”确实返回了我的应用程序类。

=>结合this到的范围testDispatcher() 那可能不是您想要的。 在这种情况下,我认为您应该删除=>表示法,而只是一个常规的匿名函数。

另外, this.setState(...args...).bind(this)完全错误。 1) this. part表示.bind(this)是多余的。 2)绑定的语法类似于: foo.setState.bind(notFoo, ...args...)

我的课不是从React Component扩展的,因此this.setState不是一个函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM