Skip to content

Instantly share code, notes, and snippets.

@reallistic
Forked from caike/todo.jsx
Last active June 26, 2017 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reallistic/7d1842e939ff3bf5a351d4d0899c7ec3 to your computer and use it in GitHub Desktop.
Save reallistic/7d1842e939ff3bf5a351d4d0899c7ec3 to your computer and use it in GitHub Desktop.
Simple Todo app demo using React + ES6
var React = require("react");
var request = require("requests");
var allItems = []
allItems.push("Buy ingredients for Crock Pot");
allItems.push("Pick up chair at IKEA");
allItems.push("Go see mom");
class TodoList extends React.Component {
constructor(props){
super(props);
this.addEvent = this.addEvent.bind(this);
}
getInitialState() {
return { allItems };
}
render() {
var items = this.props.items.map((item) => {
return <li><TodoItem item={item} /></li>;
})
return(
<div>
<ul>{items}</ul>
<p><NewTodoItem addEvent={this.addEvent} /></p>
</div>
);
}
addEvent(todoItem){
allItems.push(todoItem.newItem);
this.setState({ allItems });
}
}
class TodoItem extends React.Component {
render(){
return <div>{this.props.item}</div>;
}
}
class NewTodoItem extends React.Component {
constructor(props){
super(props);
this.onSubmit = this.onSubmit.bind(this);
}
componentDidMount(){
React.findDOMNode(this.refs.itemName).focus();
}
render(){
return (<form onSubmit={this.onSubmit}>
<input ref="itemName" type="text" />
<input ref="itemTitle" type="text" />
</form>);
}
onSubmit(event){
event.preventDefault();
var input = React.findDOMNode(this.refs.itemName)
var input2 = React.findDOMNode(this.refs.itemTitle)
var newItem = input.value;
var newItem2 = input2.value;
this.props.addEvent({ newItem });
input.value = '';
request.post("/path/to/server", {body: {
value: newItem,
value2: newItem2
}}, function(err, response) {
if (err != null) {
// react to err
}
else {
console.log(response.statusCode); // Hope for a 200
}
});
request.post("/path/to/server", {body: {
value: newItem,
value2: newItem2
}}).then(function(response) {
console.log(response.statusCode); // Hope for a 200
}).then(function(data) {
return request.post("/path/to/other/server", {});
}).then(function(response) {
console.log(response.statusCode); // Hope for a 200
}).catch(function(err) {
// react to err
});
}
}
React.render(<TodoList items={allItems} />, document.getElementById('example'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment