A Simple React App Example: Fetch GitHub Users' Information via API
A Simple React App Example: Fetch GitHub Users’ Information via API
In this article, we will walk through a simple example of a React app that fetches GitHub user information using the GitHub API. This app consists of a form where users can enter a GitHub username. When the form is submitted, the app sends a request to the GitHub API, retrieves the user information, and displays it.
To begin, let’s create a reusable component called Card that will display the user’s image and details obtained from GitHub. The component will receive its data through props and display:
Next, we create a list of Card components using the CardList component. The CardList component receives an array of cards as a prop and iterates over it using the map() function to output a list of cards.
To fetch the user information, we’ll use the Form component. The Form component manages its own state, including the username entered by the user. When the form is submitted, it sends a request to the GitHub API using Axios to retrieve the user information. We clear the input field by resetting the username state.
To add the Form component to App, we pass a method called addNewCard as the onSubmit prop. The addNewCard method adds a new card to the list of cards in App by updating the cards state.