React-Protal(传送门)

import React, { Component } from 'react';
import { createPortal } from 'react-dom';

export default class Dialog extends Component {
    constructor(props) {
        super(props);
        const doc = window.document;
        this.node = doc.createElement('div');
        doc.body.appendChild(this.node);
    }
    componentWillUnmont(){
        window.document.body.removeChild(this.node);
    }
    render(){
        const { hideDialog } = this.props;
        return createPortal(
            <div>
                {this.props.children}
                {typeof hideDialog === 'function' && (
                    <button onClick={ hideDialog }>关闭</button>
                    )
            </div>, this.node
        )
    }
}
发布了35 篇原创文章 · 获赞 1 · 访问量 6718

猜你喜欢

转载自blog.csdn.net/qq_36162529/article/details/102682926