React - uncaught TypeError: Cannot read property 'setState' of undefined

后端 未结 18 2582
清歌不尽
清歌不尽 2020-11-22 13:35

I am getting the following error

Uncaught TypeError: Cannot read property \'setState\' of undefined

even after binding delta in

18条回答
  •  爱一瞬间的悲伤
    2020-11-22 14:18

    When using ES6 code in React always use arrow functions, because the this context is automatically binded with it

    Use this:

    (videos) => {
        this.setState({ videos: videos });
        console.log(this.state.videos);
    };
    

    instead of:

    function(videos) {
        this.setState({ videos: videos });
        console.log(this.state.videos);
    };
    

提交回复
热议问题