无法访问 this.setState

Can't access this.setState

我刚刚在订阅上实现了一个有效的 Apollo 客户端侦听器。进行订阅后,下面的代码可以获取并记录它。

但是,this.setState 由于某种原因似乎不起作用。当获取订阅时,应用会抛出 TypeError: this.setState is not a function

谁能解释为什么?

    componentDidMount() {
        const SUBSCRIBE_POST = gql`
        subscription{
        postAdded{
            id
            message
            type
            created
            customers_ID
            customer{id,firstName}
        }
        }
        `;
        let response = null
        const {client} = this.props
        client.subscribe({query: SUBSCRIBE_POST})
            .subscribe({
                next(data) {
                    response = data.data.postAdded
                    console.log(response)
                    this.setState({list: response})
                },
                error(err) {console.error('err', err);},
            })
    }

在 componentDidMount 之前:

const that = this;

稍后再打电话:

that.setState();

阅读 JavaScript“this”,度过一个开明的周末。 “你还不知道 JavaScript” 是一本很好的读物。

另请阅读 React Hooks。