Reputation: 57
I'm new to React
when I'm down my soul so weary, troubles come to my code. because i can't set the state of the addTodo function. can't add new value to tasks.
my code here:
class App extends Component {
constructor(props) {
super(props)
const tasks = [
{ title: "First Proejct", id: 0 }
]
this.state = {
tasks,
uniqueId: 1
}
}
addTodo = (title) => {
const {
tasks,
uniqueId
} = this.state;
const task = {
title,
id: uniqueId,
}
const newTasks = [...tasks, task]
this.setState({
newTasks, <-- HERE. :(
uniqueId: uniqueId + 1
})
console.log(newTasks)
console.log(task)
}
render() {
return (
<div className="todoApp">
<h1>ToDO APP</h1>
<TodoInput addTodo={this.addTodo} />
<TodoList tasks={this.state.tasks} />
</div>
);
}
}
export default App;
so..
anything that I'm doing wrong?
Upvotes: 0
Views: 48
Reputation: 14405
you should invoke:
this.setState({
tasks: newTasks,
uniqueId: uniqueId + 1
});
Upvotes: 4