首页 > 其他 > 详细

Model 高阶组件

时间:2020-07-17 14:52:21      阅读:37      评论:0      收藏:0      [点我收藏+]

 

//withModel.tsx

import React, { Component } from "react";
import { Modal } from "antd";

export interface withLoadingProps {
    isLoading: boolean;
    modalProps?: any
    teamlist?: any
}

const withModel: any = WrappedComponent => {

    return class withModel extends Component<withLoadingProps> {// 继承Component
        state = {
            visibleModel: true,
            footer: []
        }

        hideModal = () => {
            this.setState({ visibleModel: false });
        };
        render() {
            const { modalProps, ...rest } = this.props;
            const { visibleModel, footer } = this.state;
            return (
                <Modal
                    visible={visibleModel}
                    className="report-modal"
                    onCancel={this.hideModal}
                    destroyOnClose={true}
                    centered={true}
                    footer={footer}
                    {...modalProps}
                >
                    <WrappedComponent {...rest} />
                </Modal>
            )
        }
    }

}
export default withModel;

 

//helper.tsx

import { unmountComponentAtNode, render as reactDomRender } from ‘react-dom‘;

class Helper {
    // 渲染弹出框model
    renderModel(component) {
        const domId = ‘tj-render-dom‘;
        let domObject = document.querySelector(‘#‘ + domId);
        if (!domObject) {
            const el = document.createElement(‘div‘);
            el.setAttribute(‘id‘, domId);
            document.querySelector(‘body‘).appendChild(el);
            domObject = el;
        }
        unmountComponentAtNode(domObject);
        reactDomRender(component, domObject);
    }
}

export default new Helper()

 

//应用

const Model = withModel(PreviewReport);    //要渲染在Model内的组件
        helper.renderModel(<Model
            modalProps={ {
                title: "预览报告",
                width: 1200,
            } }
        />);

 

Model 高阶组件

原文:https://www.cnblogs.com/Dylan-c/p/13328704.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!