javascript - ReactJS : this.setState is not a function? - Stack Overflow

I am new to ReactJS and I am having an error "this.setState is not a function".constructor()

I am new to ReactJS and I am having an error "this.setState is not a function".

constructor() {
    super();

    this.state = {
        visible: false,
        navLinesShow: true
    };

    this.navOpen = this.navOpen.bind(this)

}

navOpen() {
    this.setState({
        navStatus: "navShow",
        navLinesShow: false
    });

    if ( this.state.visible === false) {

        setTimeout(function (){

            this.setState({
                visible: true
             });

        }, 3000);

    }

I have added this.navOpen = this.navOpen.bind(this) to the contructor. So I guess the problem is with setTimeout function.

What is a possible fix?

Thank You.

I am new to ReactJS and I am having an error "this.setState is not a function".

constructor() {
    super();

    this.state = {
        visible: false,
        navLinesShow: true
    };

    this.navOpen = this.navOpen.bind(this)

}

navOpen() {
    this.setState({
        navStatus: "navShow",
        navLinesShow: false
    });

    if ( this.state.visible === false) {

        setTimeout(function (){

            this.setState({
                visible: true
             });

        }, 3000);

    }

I have added this.navOpen = this.navOpen.bind(this) to the contructor. So I guess the problem is with setTimeout function.

What is a possible fix?

Thank You.

Share Improve this question asked Jun 30, 2016 at 13:15 xoomerxoomer 3212 gold badges11 silver badges22 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Yes the problem is the setTimeout inside the setTimeout function "this" refer to the function itself: so the solution is the typical var that = this :

navOpen() {
this.setState({
    navStatus: "navShow",
    navLinesShow: false
});
if ( this.state.visible === false) {
 var that = this;
    setTimeout(function (){
        that.setState({
            visible: true
         });
    }, 3000);
}

It is because this doesn't have the correct value due to runtime binding. You need to use lexical binding. The best solution is to use ES6 arrow functions () => {} which provides lexical binding of this value. Even with the setTimeout() the lexical binding of this would fix the error you are getting

constructor() {
    super();

    this.state = {
        visible: false,
        navLinesShow: true
    };
}

navOpen = () => {
    this.setState({
        navStatus: "navShow",
        navLinesShow: false
    });

    if ( this.state.visible === false) {
        setTimeout(() => {
            this.setState({
                visible: true
             });
        }, 3000);
    }
}

Another solution in addition to @pinturic's solution is to use ES6 arrow functions. If you're using ES6/Babel, etc., you can use an arrow function to bind to the lexical this.

navOpen() {
    this.setState({
        navStatus: "navShow",
        navLinesShow: false
    });
    if (!this.state.visible) {
        setTimeout(() => this.setState({visible: true}), 3000);
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744083228a4555691.html

相关推荐

  • javascript - ReactJS : this.setState is not a function? - Stack Overflow

    I am new to ReactJS and I am having an error "this.setState is not a function".constructor()

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信