I'm starting to learn React with a to-do list mini project. It has three Components:
Tasksis the main component (child ofApp). It render aNewTaskcomponent which add new tasks and contains an array with all of theTaskcomponents to show.NewTaskis aninputto add a newTaskto the list.Taskrepresent a task
My goal is to add a new Task into the top of the list, so I have thought that the best way to do this is to add the Task to the start of the array of Tasks. Here is the problem, when I do this the render doesn't display the tasks correctly, and repeat the last one. When I add the task to the last element the render results are correct. Here some pictures:
(At the left not correct and at the right correct)
Here is the code of the three components:
class Tasks extends React.Component{
constructor(props){
super(props);
this.state = {
tasks: [<Task text="Comprar cerveza"/>,
<Task text="Hacer la comida"/>,
<Task text="Ver HIMYM"/>,
<Task text="Escuchar nuevo disco"/>]
}
}
addNewTask = (text) => {
// this.setState({tasks: this.state.tasks.concat(<Task text={text}/>)}) // Add to bottom
this.setState({tasks: [<Task text={text}/>].concat(this.state.tasks)}) // Add to top
}
render(){
return (
<div className="task-container">
<NewTask parentMethod={this.addNewTask} text="New Task"/>
{this.state.tasks}
</div>
);
}
}
class NewTask extends React.Component{
constructor(props){
super(props);
}
handleKeyPress = (event) => {
if(event.key === 'Enter'){
this.props.parentMethod(event.target.value);
}
}
render(){
return (
<div className="task">
<input className="new-task" placeholder={this.props.text} onKeyPress={this.handleKeyPress}/>
</div>
);
}
}
class Task extends React.Component{
constructor(props){
super(props);
this.state = {
completed: false
}
}
changeState(){
this.setState({completed: !this.state.completed});
}
render(){
let class_button = "check-button " + (this.state.completed ? 'completed' : 'not-completed');
let class_text = "posted-task " + (this.state.completed ? 'completed' : 'not-completed');
return (
<div className="task">
<button className={class_button} onClick={this.changeState.bind(this)}/>
<input className={class_text} defaultValue={this.props.text}/>
</div>
);
}
}

