beltchecker react demo
by freemanstu
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
.done {
color: rgba(0, 0, 0, 0.3);
text-decoration: line-through;
}
input {
margin: 5px;
}
React
class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {user: {}};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleSubmit(event) {
fetch('https://www.beltchecker.com/api/user.php?id=' + this.state.id)
.then(res => res.json())
.then(user => this.setState({user}));
event.preventDefault();
}
handleChange(event) { this.setState({id: event.target.value})}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>id:</label>
<input value={this.state.id} onChange={this.handleChange}/>
<input type="submit" value="Lookup"/>
</form>
<h2>{this.state.user.firstname} {this.state.user.lastname}</h2>
<img src={this.state.user.image}/>
</div>
)
}
}
ReactDOM.render(<Profile/>, document.querySelector("#app"));