-
Notifications
You must be signed in to change notification settings - Fork 13.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React - Improve performance by 6x #693
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,17 +43,15 @@ | |
} | ||
|
||
var val = this.refs.newField.getDOMNode().value.trim(); | ||
var todos; | ||
var newTodo; | ||
|
||
if (val) { | ||
todos = this.state.todos; | ||
newTodo = { | ||
id: Utils.uuid(), | ||
title: val, | ||
completed: false | ||
}; | ||
this.setState({todos: todos.concat([newTodo])}); | ||
this.setState({todos: this.state.todos.concat([newTodo])}); | ||
this.refs.newField.getDOMNode().value = ''; | ||
} | ||
|
||
|
@@ -63,16 +61,22 @@ | |
toggleAll: function (event) { | ||
var checked = event.target.checked; | ||
|
||
this.state.todos.forEach(function (todo) { | ||
todo.completed = checked; | ||
var newTodos = this.state.todos.map(function (todo) { | ||
return Utils.extend({}, todo, {completed: checked}); | ||
}); | ||
|
||
this.setState({todos: this.state.todos}); | ||
this.setState({todos: newTodos}); | ||
}, | ||
|
||
toggle: function (todo) { | ||
todo.completed = !todo.completed; | ||
this.setState({todos: this.state.todos}); | ||
var newTodos = this.state.todos.map(function (t) { | ||
if (t !== todo) { | ||
return t; | ||
} | ||
return Utils.extend({}, t, {completed: !todo.completed}); | ||
}); | ||
|
||
this.setState({todos: newTodos}); | ||
}, | ||
|
||
destroy: function (todo) { | ||
|
@@ -92,8 +96,14 @@ | |
}, | ||
|
||
save: function (todo, text) { | ||
todo.title = text; | ||
this.setState({todos: this.state.todos, editing: null}); | ||
var newTodos = this.state.todos.map(function (t) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be a bit cleaner: Thoughts?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer the shorter one. @sindresorhus? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
if (t !== todo) { | ||
return t; | ||
} | ||
return Utils.extend({}, t, {title: text}); | ||
}); | ||
|
||
this.setState({todos: newTodos, editing: null}); | ||
}, | ||
|
||
cancel: function () { | ||
|
@@ -115,9 +125,6 @@ | |
render: function () { | ||
var footer = null; | ||
var main = null; | ||
var todoItems = {}; | ||
var activeTodoCount; | ||
var completedCount; | ||
|
||
var shownTodos = this.state.todos.filter(function (todo) { | ||
switch (this.state.nowShowing) { | ||
|
@@ -128,11 +135,12 @@ | |
default: | ||
return true; | ||
} | ||
}.bind(this)); | ||
}, this); | ||
|
||
shownTodos.forEach(function (todo) { | ||
todoItems[todo.id] = ( | ||
var todoItems = shownTodos.map(function (todo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! |
||
return ( | ||
<TodoItem | ||
key={todo.id} | ||
todo={todo} | ||
onToggle={this.toggle.bind(this, todo)} | ||
onDestroy={this.destroy.bind(this, todo)} | ||
|
@@ -142,13 +150,13 @@ | |
onCancel={this.cancel} | ||
/> | ||
); | ||
}.bind(this)); | ||
}, this); | ||
|
||
activeTodoCount = this.state.todos.filter(function (todo) { | ||
var activeTodoCount = this.state.todos.filter(function (todo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be great (and memory conservative) to be able to do this without filter. |
||
return !todo.completed; | ||
}).length; | ||
|
||
completedCount = this.state.todos.length - activeTodoCount; | ||
var completedCount = this.state.todos.length - activeTodoCount; | ||
|
||
if (activeTodoCount || completedCount) { | ||
footer = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
} | ||
return false; | ||
}, | ||
|
||
handleEdit: function () { | ||
// react optimizes renders by batching them. This means you can't call | ||
// parent's `onEdit` (which in this case triggeres a re-render), and | ||
|
@@ -33,6 +34,7 @@ | |
node.focus(); | ||
node.setSelectionRange(node.value.length, node.value.length); | ||
}.bind(this)); | ||
this.setState({editText: this.props.todo.title}); | ||
}, | ||
|
||
handleKeyDown: function (event) { | ||
|
@@ -41,8 +43,6 @@ | |
this.props.onCancel(); | ||
} else if (event.keyCode === ENTER_KEY) { | ||
this.handleSubmit(); | ||
} else { | ||
this.setState({editText: event.target.value}); | ||
} | ||
}, | ||
|
||
|
@@ -54,33 +54,36 @@ | |
return {editText: this.props.todo.title}; | ||
}, | ||
|
||
componentWillReceiveProps: function (nextProps) { | ||
if (nextProps.todo.title !== this.props.todo.title) { | ||
this.setState(this.getInitialState()); | ||
} | ||
shouldComponentUpdate: function (nextProps, nextState) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to have a comment here saying something like:
|
||
return ( | ||
nextProps.todo.id !== this.props.todo.id || | ||
nextProps.todo !== this.props.todo || | ||
nextProps.editing !== this.props.editing || | ||
nextState.editText !== this.state.editText | ||
); | ||
}, | ||
|
||
render: function () { | ||
return ( | ||
<li class={Utils.stringifyObjKeys({ | ||
<li className={Utils.stringifyObjKeys({ | ||
completed: this.props.todo.completed, | ||
editing: this.props.editing | ||
})}> | ||
<div class="view"> | ||
<div className="view"> | ||
<input | ||
class="toggle" | ||
className="toggle" | ||
type="checkbox" | ||
checked={this.props.todo.completed ? 'checked' : null} | ||
onChange={this.props.onToggle} | ||
/> | ||
<label onDoubleClick={this.handleEdit}> | ||
{this.props.todo.title} | ||
</label> | ||
<button class='destroy' onClick={this.props.onDestroy} /> | ||
<button className="destroy" onClick={this.props.onDestroy} /> | ||
</div> | ||
<input | ||
ref="editField" | ||
class="edit" | ||
className="edit" | ||
value={this.state.editText} | ||
onBlur={this.handleSubmit} | ||
onChange={this.handleChange} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use one char variable names