如何用玩笑来对componentWillReceiveProps(nextProps)进行单元测试?

componentWillReceiveProps(nextProps) {
    if (nextProps.data === undefined ||
        nextProps.data.hasOwnProperty('error') ||
        nextProps.err !== undefined) {
        this.setState({
        messageError: "Something went wrong!"
        });
    }  else if ((nextProps.data).length) {
        this.setState({
            dataArray: nextProps.data
        });
    }
}

最佳答案

我认为这是您对问题https://medium.com/@pchomphoosang/react-js-how-to-do-unit-testing-on-componentdidmount-componentwillreceiveprops-866385c3e5dc的回答

但是我建议您将代码从componentWillReceiveProps移到shouldComponentUpdate,因为不建议使用CWRP。

10-07 21:00