Using the Fetch API
Let’s explore how we can retrieve data from a server in practice. We’ll start with the Fetch API, the most common and fundamental approach provided by web browsers.
Before we begin, let’s create a small application that fetches user data from GitHub and displays their avatar and basic information on the screen. To do this, we’ll need an empty Vite project with React. You can create it with the following command:
npm create vite@latest
Since we’re using TypeScript in our examples, let’s start by defining the GitHubUser
interface and all the necessary parameters.
To find out what data the server returns, we often need to refer to the documentation, usually provided by backend developers. In our case, since we’re using the GitHub REST API, we can find user information in the official GitHub documentation at this link: https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28.
Let’...