From the course: React: Components, Context, and Accessibility

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Solution: Implementing array-based state updates

Solution: Implementing array-based state updates - React.js Tutorial

From the course: React: Components, Context, and Accessibility

Solution: Implementing array-based state updates

(upbeat music) - [Instructor] Now that we have our task list scaffolded here, we can create our handleAddTask function that's going to add these values every time I click on Add Task. So let's go ahead and replace those comments with this. We'll say const newTask, and we'll set that equal to New Task. And then we'll place the this.state.count value right here, make sure that semicolon is the outside of those back ticks. Then we'll call this.setState. We'll take in the previous state. And then we want to return an object. So we need to wrap that in a set of parentheses and we'll say tasks. And then we'll push in our previous State.tasks and add the newTask. And then we'll also set the count to whatever the prevState.count was, plus one. Next, we want to render the tasks. So in our unordered list, we will map over the values that are in state. this.state.tasks.map. And then we will pass task and index in here. And then we will return a list item. Each list item should have the task…

Contents