Skip to content

More React!

Posted on:April 22, 2024

Project: To-do list (20min)

We will make a to-do list in React that can add tasks and mark tasks as completed.

Checkout the finished web page here

Let’s break it down step by step:

  1. Assume we have an array of tasks, how do we display them on the page? Hint: Use the map function

  2. There is an input box on the web page to get the user input of tasks, how do we read the input from the user? Hint: the onChange event

<input
  type="text"
  placeholder="Add a todo"
  onChange={event => {
    /** Do something here **/
    // event.target.value will give you the value of the input
  }}
/>
  1. To keep track of the user input, we need to store it in a state. How do we do that? Hint: Use the useState hook

  2. How can we modify and keep track of the list of tasks? Hint: Use another useState hook with arrays!

  3. How do we add a task to the list of tasks? Hint: array.concat() function

  4. How do we mark a task as completed? Hint: Use the array.filter() function to modify the tasks list

setTodoArr(
  todoArr.filter((_, todoIndex) => {
    return todoIndex !== index;
  })
);

Extras:

  1. How do we animate the component? I used AutoAnimate, try to figure out how to use it!

Now that we have a to-do list, let’s move on to the next topic.

Requesting the Web

What is web request?

Different ways of client-server communication

Restful API

Built on top of HTTP/s protocol.

When a client request is made via a RESTful API, information is delivered in one of several formats via HTTP: JSON (Javascript Object Notation), HTML, XLT, or plain text. JSON is the most generally popular file format to use because, despite its name, it’s language-agnostic, as well as readable by both humans and machines.

Try it out

Fetch API

const response = await fetch("https://www.boredapi.com/api/activity");

But what is await?

Asynchronous programming

Why do we need asynchronous programming?

Sending a request to a server takes time, and we don’t want to block the main thread while waiting for the response.

More about requests

fetch("https://example.com/", {
  method: "GET",
  body: "Some data",
});

More about responses

Do you know JSON?

{
  "firstName": "Duke",
  "lastName": "Java",
  "age": 18,
  "streetAddress": "100 Internet Dr",
  "city": "JavaTown",
  "state": "JA",
  "postalCode": "12345",
  "phoneNumbers": [{ "Mobile": "111-111-1111" }, { "Home": "222-222-2222" }]
}