В приложении отправьте метод closeWindowPortal в качестве опоры в Window
<Window closeWindowPortal={() => this.closeWindowPortal()}>
<CounterButton />
<button onClick={() => this.closeWindowPortal()}>Close</button>
</Window>
, затем замените компонент Window этим
class Window extends React.Component {
constructor(props) {
super(props);
this.state = { win: null, el: null };
this.hendleClose = this.hendleClose.bind(this);
}
componentDidMount() {
let win = window.open('', '', 'width=600,height=400');
win.document.title = 'A React portal window';
let el = document.createElement('div');
win.document.body.appendChild(el);
this.setState({ win, el });
win.onbeforeunload = this.hendleClose
}
hendleClose(){
this.props.closeWindowPortal()
}
componentWillUnmount() {
this.state.win.close();
}
render() {
const { el } = this.state;
if (!el) {
return null;
}
return ReactDOM.createPortal(this.props.children, el);
}
}