Skip to content

Instantly share code, notes, and snippets.

@chochinlu
Created August 18, 2014 03:10
Show Gist options
  • Save chochinlu/07845311f5d9736bf6cc to your computer and use it in GitHub Desktop.
Save chochinlu/07845311f5d9736bf6cc to your computer and use it in GitHub Desktop.
TodoApp react component to demonstrate react lifecycle. sample from : http://modernweb.com/2014/07/23/getting-started-reactjs/
/** @jsx React.DOM */
var React = require('react'),
TodoList = require('./todo-list');
var TodoApp = React.createClass({
getInitialState: function(){
console.log('TodoApp -- getInitialState');
return {
items: [],
text: ''
};
},
render: function(){
console.log('TodoApp -- render');
return (
<div>
<h3>TODO</h3>
<TodoList items={this.state.items} />
<form onSubmit={this._handleSubmit}>
<input onChange={this._onChange} value={this.state.text} />
<button>{'Add #' + (this.state.items.length+1)}</button>
</form>
</div>
);
},
_onChange: function(e){
this.setState({text: e.target.value});
},
_handleSubmit: function(e){
e.preventDefault();
var nextItems = this.state.items.concat(this.state.text);
var nextText = '';
this.setState({
items: nextItems,
text: nextText
});
}
});
React.renderComponent(
<TodoApp />,
document.getElementById('app'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment