我收到一个websocket消息。我想用此消息更改组件的状态。当我尝试执行此操作时,出现错误。我该如何解决?

componentDidMount() {

    this.wsConnection.onmessage = function (eventInfo) {

        console.log("Message arrived from websocket: ", eventInfo.data);
        this.setState({team: eventInfo.data});
    };
}

我的错误:
TypeError: this.setState is not a function

最佳答案

你可以尝试以下代码

this.wsConnection.onmessage = (eventInfo) => {

    console.log("Message arrived from websocket: ", eventInfo.data);
    this.setState({team: eventInfo.data});
};

10-04 15:11