const ToDo = props => (
|
|
|
);
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 (
key=id
ID |
|
created at |
{this.state.list.map(
(todo, index) => (
)
)}
);
}
}
ReactDOM.render(
,
document.getElementById('root')
);