0

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?

1 Answer 1

4

you should invoke:

this.setState({
  tasks: newTasks,
  uniqueId: uniqueId + 1
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.