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