Last active
November 10, 2017 19:05
-
-
Save davidgilbertson/69a8c158ca9b5e202d083408dbabdf49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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