forked from reactjs/react.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move reconciliation index as keys examples from codepen into ./examples
- Loading branch information
Showing
3 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const ToDo = (props) => ( | ||
<tr> | ||
<td><label>{props.id}</label></td> | ||
<td><input/></td> | ||
<td><label>{props.createdAt.toTimeString()}</label></td> | ||
</tr> | ||
); | ||
|
||
class ToDoList extends React.Component { | ||
constructor() { | ||
super(); | ||
const date = new Date(); | ||
const todoCounter = 1; | ||
this.state = { | ||
todoCounter: todoCounter, | ||
list: [ | ||
{ id: todoCounter, createdAt: date }, | ||
] | ||
} | ||
} | ||
|
||
sortByEarliest() { | ||
const sortedList = this.state.list.sort((a, b) => { | ||
return a.createdAt - b.createdAt; | ||
}); | ||
this.setState({ | ||
list: [...sortedList] | ||
}) | ||
} | ||
|
||
sortByLatest() { | ||
const sortedList = this.state.list.sort((a, b) => { | ||
return b.createdAt - a.createdAt; | ||
}); | ||
this.setState({ | ||
list: [...sortedList] | ||
}) | ||
} | ||
|
||
addToEnd() { | ||
const date = new Date(); | ||
const nextId = this.state.todoCounter + 1; | ||
const newList = [ | ||
...this.state.list, | ||
{ id: nextId, createdAt: date } | ||
]; | ||
this.setState({ | ||
list: newList, | ||
todoCounter: nextId | ||
}); | ||
} | ||
|
||
addToStart() { | ||
const date = new Date(); | ||
const nextId = this.state.todoCounter + 1; | ||
const newList = [ | ||
{ id: nextId, createdAt: date }, | ||
...this.state.list | ||
]; | ||
this.setState({ | ||
list: newList, | ||
todoCounter: nextId | ||
}); | ||
} | ||
|
||
render() { | ||
return( | ||
<div> | ||
<code>key=index</code><br/> | ||
<button onClick={this.addToStart.bind(this)}> | ||
Add New to Start | ||
</button> | ||
<button onClick={this.addToEnd.bind(this)}> | ||
Add New to End | ||
</button> | ||
<button onClick={this.sortByEarliest.bind(this)}> | ||
Sort by Earliest | ||
</button> | ||
<button onClick={this.sortByLatest.bind(this)}> | ||
Sort by Latest | ||
</button> | ||
<table> | ||
<tr> | ||
<th>ID</th><th></th><th>created at</th> | ||
</tr> | ||
{ | ||
this.state.list.map((todo, index) => ( | ||
<ToDo | ||
key={index} | ||
{...todo} | ||
/> | ||
)) | ||
} | ||
</table> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
ReactDOM.render( | ||
<ToDoList />, | ||
document.getElementById('root') | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const ToDo = (props) => ( | ||
<tr> | ||
<td><label>{props.id}</label></td> | ||
<td><input/></td> | ||
<td><label>{props.createdAt.toTimeString()}</label></td> | ||
</tr> | ||
); | ||
|
||
class ToDoList extends React.Component { | ||
constructor() { | ||
super(); | ||
const date = new Date(); | ||
const toDoCounter = 1; | ||
this.state = { | ||
list: [ | ||
{ id: toDoCounter, createdAt: date }, | ||
], | ||
toDoCounter: toDoCounter | ||
} | ||
} | ||
|
||
sortByEarliest() { | ||
const sortedList = this.state.list.sort((a, b) => { | ||
return a.createdAt - b.createdAt; | ||
}); | ||
this.setState({ | ||
list: [...sortedList] | ||
}) | ||
} | ||
|
||
sortByLatest() { | ||
const sortedList = this.state.list.sort((a, b) => { | ||
return b.createdAt - a.createdAt; | ||
}); | ||
this.setState({ | ||
list: [...sortedList] | ||
}) | ||
} | ||
|
||
addToEnd() { | ||
const date = new Date(); | ||
const nextId = this.state.toDoCounter + 1; | ||
const newList = [ | ||
...this.state.list, | ||
{ id: nextId, createdAt: date } | ||
]; | ||
this.setState({ | ||
list: newList, | ||
toDoCounter: nextId | ||
}); | ||
} | ||
|
||
addToStart() { | ||
const date = new Date(); | ||
const nextId = this.state.toDoCounter + 1; | ||
const newList = [ | ||
{ id: nextId, createdAt: date }, | ||
...this.state.list | ||
]; | ||
this.setState({ | ||
list: newList, | ||
toDoCounter: nextId | ||
}); | ||
} | ||
|
||
render() { | ||
return( | ||
<div> | ||
<code>key=id</code><br/> | ||
<button onClick={this.addToStart.bind(this)}> | ||
Add New to Start | ||
</button> | ||
<button onClick={this.addToEnd.bind(this)}> | ||
Add New to End | ||
</button> | ||
<button onClick={this.sortByEarliest.bind(this)}> | ||
Sort by Earliest | ||
</button> | ||
<button onClick={this.sortByLatest.bind(this)}> | ||
Sort by Latest | ||
</button> | ||
<table> | ||
<tr> | ||
<th>ID</th><th></th><th>created at</th> | ||
</tr> | ||
{ | ||
this.state.list.map((todo, index) => ( | ||
<ToDo | ||
key={todo.id} | ||
{...todo} | ||
/> | ||
)) | ||
} | ||
</table> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
ReactDOM.render( | ||
<ToDoList />, | ||
document.getElementById('root') | ||
); |