Using Fetch to Interact with Unauthenticated REST API Endpoints
Learn about how to interact with unauthenticated REST API endpoints using fetch function in React.
We'll cover the following
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 to interact with our REST API. If you haven't already, let's open Visual Studio Code and carry out the following steps.
Open QuestionsData.ts
, find the getUnansweredQuestions
function, and replace the implementation with the following content:
Get hands-on with 1400+ tech skills courses.