Skip to content

Instantly share code, notes, and snippets.

@debonx
Last active January 2, 2020 17:21
Show Gist options
  • Save debonx/6bbec7ec0d056d96c1bda924750eb0ef to your computer and use it in GitHub Desktop.
Save debonx/6bbec7ec0d056d96c1bda924750eb0ef to your computer and use it in GitHub Desktop.
React: Password form component.
import React from 'react';
import ReactDOM from 'react-dom';
class Follow extends React.Component {
constructor(props) {
super(props);
this.state = {
password: 'huraji',
authorized: false
};
this.authorize = this.authorize.bind(this);
}
authorize(e) {
const password = e.target.querySelector(
'input[type="password"]').value;
const auth = password == this.state.password;
this.setState({
authorized: auth
});
}
render() {
const login = (
<form action="#" onSubmit={this.authorize}>
<input type="password" placeholder="Password" />
<input type="submit" />
</form>
);
const contactInfo = (
<ul>
<li>
follow me https://twitter.com/_huraji
</li>
</ul>
);
return (
<div id="authorization">
<h1>{ this.state.authorized ? 'Contact' : 'Enter the Password' }</h1>
{ this.state.authorized ? contactInfo : login }
</div>
);
}
}
ReactDOM.render(
<Follow />,
document.getElementById('app')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment