Skip to content

Instantly share code, notes, and snippets.

@bendozy
Created May 27, 2017 11:13
Show Gist options
  • Save bendozy/e186f91a4e6c7b38c45ab2ae0df0c638 to your computer and use it in GitHub Desktop.
Save bendozy/e186f91a4e6c7b38c45ab2ae0df0c638 to your computer and use it in GitHub Desktop.
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