Created
May 27, 2017 11:13
-
-
Save bendozy/e186f91a4e6c7b38c45ab2ae0df0c638 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
import React from 'react'; | |
import {render} from 'react-dom'; | |
const AuthenticationHOC = (Component) => class extends React.Component { | |
constructor(props){ | |
super(props); | |
this.state = { | |
currentUser: {}, | |
isAuthenticating: false, | |
isLoading: false | |
} | |
this.login = this.login.bind(this); | |
} | |
login (e){ | |
e.preventDefault(); | |
const username = e.target.username.value; | |
const password = e.target.password.value; | |
console.log(username, password); | |
this.setState({currentUser: {username, password}}); | |
} | |
render() { | |
return <Component {...this.state} login={this.login}/> | |
} | |
} | |
const AuthenticationCompnent = (props) => { | |
return ( | |
<div> | |
<p> {props.currentUser.username} </p> | |
<p> {props.currentUser.password} </p> | |
<form onSubmit={props.login}> | |
<div> | |
Username <input name="username"/> | |
</div> | |
<div> | |
Password <input name="password" type="password"/> | |
</div> | |
<div> | |
<button type="submit"> Login </button> | |
</div> | |
</form> | |
</div>); | |
} | |
const DecoratedAuthComponent = AuthenticationHOC(AuthenticationCompnent); | |
render(<DecoratedAuthComponent />, document.querySelector('#app')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment