React
by shahrukh007
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-right: 5px;
}
React
class TodoApp extends React.Component {
constructor(props) {
super(props)
this.state = {list:[],
userInput: '',
}
this.addItem = this.addItem.bind(this);
}
addItem(e) {
this.setState({userInput: event.target.value});
}
getData(e) {
e.preventDefault();
var self=this;
$.ajax({
type: "get",
url: 'https://api.github.com/users/funchal',
async: true,
success: function(response){
self.state.list.push(response)
self.setState({list:self.state.list
})
},
error: function (error) {
alert(error);
}
});
}
render() {
console.log(this.state.list);
return (
<div>
<form onSubmit={this.getData}>
<input type="text" value={this.state.userInput} onChange={this.addItem} required/>
<input type="submit" value="Submit" />
</form>
<h2>User List</h2>
<ul>
{this.state.list.map((i,index)=>
<li key={index}>{i.id}</li>
)}
</ul>
</div>
)
}
}
ReactDOM.render(<TodoApp />, document.querySelector("#app"))