Skip to content

Instantly share code, notes, and snippets.

@davidgilbertson
Last active November 10, 2017 19:05
Show Gist options
  • Save davidgilbertson/69a8c158ca9b5e202d083408dbabdf49 to your computer and use it in GitHub Desktop.
Save davidgilbertson/69a8c158ca9b5e202d083408dbabdf49 to your computer and use it in GitHub Desktop.
class App extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
counter: 0,
showWindowPortal: false,
};
this.toggleWindowPortal = this.toggleWindowPortal.bind(this);
}
componentDidMount() {
window.setInterval(() => {
this.setState(state => ({
...state,
counter: state.counter + 1,
}));
}, 1000);
}
toggleWindowPortal() {
this.setState(state => ({
...state,
showWindowPortal: !state.showWindowPortal,
}));
}
render() {
return (
<div>
<h1>Counter: {this.state.counter}</h1>
<button onClick={this.toggleWindowPortal}>
{this.state.showWindowPortal ? 'Close the' : 'Open a'} Portal
</button>
{this.state.showWindowPortal && (
<MyWindowPortal>
<h1>Counter in a portal: {this.state.counter}</h1>
<p>Even though I render in a different window, I share state!</p>
<button onClick={() => this.setState({ showWindowPortal: false })} >
Close me!
</button>
</MyWindowPortal>
)}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment