在React ComponenetDidUpdate()中,生命周期方法在渲染道具模式中被多次调用

时间:2019-06-11 08:14:57

标签: reactjs typescript react-redux jsx react-tsx

我正在尝试使用render prop模式来提高代码的可重用性,为此,我遵循包含生命周期方法以及FC(F)中的clickHandler的render prop组件(R),该组件进行一些处理并更新redux商店。 我使用不同的道具在(A)组件内部使用了渲染组件(R)3次。一旦我从FC使用clickHandler,它就会从渲染道具component(R)调用方法并更新存储,但是渲染道具component(R)中的componentDidUpdate()方法也被调用了3次,这种情况不应该发生,应该只调用一次一旦。无法知道并获取哪个clickHandler更新了商店。

渲染道具component(R)中的

ComponentDidUpdate和handleUpload


/**
 * @remarks
 * call the `{uploadApiDispatcher}` with upload type and required params
 */
    handleUpload() {
        const { uploadApiDispatcher, uploadType } = this.props;
        const params: IUploadApiActionProp = {
            data: { name: 'sanjose' },
            fileIds: [1],
            uploadType,
        };
        uploadApiDispatcher(params);
    }

componentDidUpdate(prevProps, prevState) {
        const { remarks, uploadErrorMessage, uploading, uploadType } = this.props
        if(uploadType !== prevProps.uploadType){
            if (!uploading && remarks && remarks.length) {
            }
            if (!uploading && uploadErrorMessage) {
                message.error(uploadErrorMessage);
            }
        }
    }

在组件(A)中使用渲染道具功能(UploadRender)

<Col xs={24} sm={24} md={12} lg={8} xl={6}>
                                    <UploadRender
                                        render={props => (
                                            <UploadCard
                                                title="Upload admin users"
                                                subtitle="This will upload the admin users"
                                                {...props}
                                            />
                                        )}
                                        uploadType={uploadTypes.UPLOADADMINUSERS}
                                    />
                            </Col>
                            <Col xs={24} sm={24} md={12} lg={8} xl={6}>

                                    <UploadRender
                                        render={props => (
                                            <UploadCard
                                                title="Upload admin group"
                                                subtitle="This will upload the admin groups"
                                                {...props}
                                            />
                                        )}
                                        uploadType={uploadTypes.UPLOADADMINGROUPS}
                                    />
                            </Col>
                            <Col xs={24} sm={24} md={12} lg={8} xl={6}>

                                    <UploadRender
                                        render={props => (
                                            <UploadCard
                                                title="Upload internal users"
                                                subtitle="This will upload the internal users"
                                                {...props}
                                            />
                                        )}
                                        uploadType={uploadTypes.UPLOADINTERNAL}
                                    />
                            </Col>

请帮助我理解componentDidUpdate被多次调用,以及如何知道哪个clickHandler被调用。

0 个答案:

没有答案