Updating Data
Let’s add the functionality to update a task’s completion status. We’ll start by defining a function to handle the update.
Code Explanation
- Before the
useEffect
hook we added thesetCompleted
function, which takes atask
and acompleted
status as arguments. - The
taskRepo.update
method updates thecompleted
status of the given task. This makes a REST API call to the backend to update the task in the database. - After updating the task, we update the
tasks
state with the updated task, replacing the old task in the list.
Next, let’s modify the JSX to call the setCompleted
function when the checkbox is toggled.
Code Explanation
- We added an
onChange
handler to the checkbox input that calls thesetCompleted
function when the checkbox is toggled. - The
onChange
handler passes thetask
and the newchecked
status of the checkbox to thesetCompleted
function.
This code results in the following REST API request to update the task:
PUT /api/tasks/{taskId}
Try toggling the completion status of tasks using the checkboxes in the preview window below.
Files
Preparing Environment
- Installing dependencies
- Starting http server