我试图用门户打开一个新标签,问题是该标签不显示我想要获得上一个的内容,我关注这篇文章并且标签完美打开,但不显示内容,看起来像这样:
这是我的反应门户代码:
import ReactDOM, {PureComponent} from 'react';
export default class Portal extends PureComponent{
constructor(props) {
super(props);
this.containerEl = null;
this.externalWindow = null;
}
componentDidMount() {
this.externalWindow = window.open('', '', 'width=600,height=400,left=200,top=200');
this.containerEl = this.externalWindow.document.createElement('div');
this.externalWindow.document.body.appendChild(this.containerEl);
}
componentWillUnmount() {
this.externalWindow.close();
}
render() {
if (!this.containerEl) {
return null;
}
return ReactDOM.createPortal(this.props.children,this.containerEl);
}
}
这是我打开新标签的代码:
{this.state.showWindowPortal && (
<Portal closeWindowPortal={this.closeWindowPortal} >
<p>Hello</p>
<button onClick={() => this.closeWindowPortal()} >Close me!
</button>
</Portal>)}
希望大家能帮帮我,谢谢!