mirror of
https://github.com/zhigang1992/reactfire.git
synced 2026-06-13 09:35:11 +08:00
The inner bind is useless create-react-apps 'default webpack config was what pointed out this issue.
82 lines
2.0 KiB
JavaScript
82 lines
2.0 KiB
JavaScript
var TodoList2 = React.createClass({
|
|
render: function() {
|
|
var _this = this;
|
|
var createItem = function(item, index) {
|
|
return (
|
|
<li key={ index }>
|
|
{ item.text }
|
|
<span onClick={ _this.props.removeItem.bind(null, item['.key']) }
|
|
style={{ color: 'red', marginLeft: '10px', cursor: 'pointer' }}>
|
|
X
|
|
</span>
|
|
</li>
|
|
);
|
|
};
|
|
return <ul>{ this.props.items.map(createItem) }</ul>;
|
|
}
|
|
});
|
|
|
|
var TodoApp2 = React.createClass({
|
|
getInitialState: function() {
|
|
return {
|
|
items: [],
|
|
text: ''
|
|
};
|
|
},
|
|
|
|
componentWillMount: function() {
|
|
this.firebaseRef = firebase.database().ref('todoApp/items');
|
|
this.firebaseRef.limitToLast(25).on('value', function(dataSnapshot) {
|
|
var items = [];
|
|
dataSnapshot.forEach(function(childSnapshot) {
|
|
var item = childSnapshot.val();
|
|
item['.key'] = childSnapshot.key;
|
|
items.push(item);
|
|
});
|
|
|
|
this.setState({
|
|
items: items
|
|
});
|
|
}.bind(this));
|
|
},
|
|
|
|
componentWillUnmount: function() {
|
|
this.firebaseRef.off();
|
|
},
|
|
|
|
onChange: function(e) {
|
|
this.setState({text: e.target.value});
|
|
},
|
|
|
|
removeItem: function(key) {
|
|
var firebaseRef = firebase.database().ref('todoApp/items');;
|
|
firebaseRef.child(key).remove();
|
|
},
|
|
|
|
handleSubmit: function(e) {
|
|
e.preventDefault();
|
|
if (this.state.text && this.state.text.trim().length !== 0) {
|
|
this.firebaseRef.push({
|
|
text: this.state.text
|
|
});
|
|
this.setState({
|
|
text: ''
|
|
});
|
|
}
|
|
},
|
|
|
|
render: function() {
|
|
return (
|
|
<div>
|
|
<TodoList2 items={ this.state.items } removeItem={ this.removeItem } />
|
|
<form onSubmit={ this.handleSubmit }>
|
|
<input onChange={ this.onChange } value={ this.state.text } />
|
|
<button>{ 'Add #' + (this.state.items.length + 1) }</button>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|
|
});
|
|
|
|
ReactDOM.render(<TodoApp2 />, document.getElementById('todoApp2'));
|