Search⌘ K
AI Features

Using Fetch to Interact with Unauthenticated REST API Endpoints

Explore using the native fetch function in React to interact with unauthenticated REST API endpoints. Learn to retrieve unanswered questions, handle async operations, map JSON data to expected types, and centralize API calls for easier maintenance.

We are going to use the native fetch function to get unanswered questions from our real REST API. We are then going to use a wrapper function over fetch to make interacting with our backend a little easier. This approach will also centralize the code that interacts with the REST API, which is beneficial when we want to make improvements to it. We'll then move on to using the real REST API to get a single question and search for questions.

Getting unanswered questions from the REST API

We are going to start interacting with the REST API on the home page when displaying the list of unanswered questions. The HomePage component won't actually change, but the getUnansweredQuestions function in QuestionsData.ts will.

In getUnansweredQuestions, we'll leverage the native browser fetch function ...