Last active
September 7, 2020 09:33
-
-
Save anil-pace/e2d174a2a3a7b7642394b0a9b71d8d7a to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Component { | |
| constructor(props) { | |
| super(props) | |
| this.state = { | |
| isLoggedIn: false | |
| } | |
| } | |
| toggleAuth = () => { | |
| this.setState((prevState, props) => ({ isLoggedIn: !prevState.isLoggedIn })) | |
| } | |
| render() { | |
| const { isLoggedIn } = this.state | |
| return ( | |
| <div> | |
| <button onClick={this.toggleAuth}>{isLoggedIn ? 'Logout' : 'Login' }</button> | |
| <WrappedOne isLoggedIn={isLoggedIn} /> | |
| <WrappedTwo isLoggedIn={isLoggedIn} /> | |
| <WrappedThree isLoggedIn={isLoggedIn} /> | |
| </div> | |
| ); | |
| } | |
| } | |
| function AuthWrapper(WrappedComponent) { | |
| return class extends React.Component { | |
| render() { | |
| if (this.props.isLoggedIn) { | |
| return <WrappedComponent {...this.props} /> | |
| } | |
| return <p>You're not logged in ☹️</p> | |
| } | |
| } | |
| } | |
| class RegularComponent extends React.Component { | |
| render() { | |
| return <p> Regular Component</p> | |
| } | |
| } | |
| class OtherRegularComponent extends React.Component { | |
| render() { | |
| return <p> Other Regualar Component </p> | |
| } | |
| } | |
| const FunctionalComponent = () => (<p> functional component </p>) | |
| const WrappedOne = AuthWrapper(RegularComponent) | |
| const WrappedTwo = AuthWrapper(OtherRegularComponent) | |
| const WrappedThree = AuthWrapper(FunctionalComponent) | |
| ReactDOM.render( | |
| <App />, | |
| document.getElementById('app') | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment