Tech

Fetch Data in React.js

In React, there are a variety of ways to get data from an external API. The following are the top three methods for doing so.

  1. How to Use React to Fetch Data Fetching data with the Fetch API

The Get API is the most accessible way to fetch data in React js.

The Fetch API is a utility that runs on the window object (window.fetch) in most current browsers and allows us to make HTTP requests with ease using JavaScript promises.

We just need to supply the URL endpoint to which we wish to make our request when using fetch to create a basic GET request. Once our React component has been installed, we’d like to make this request.

To do this, we use the useEffect hook and pass an empty dependencies array as the second parameter, ensuring that our request is only made once (assuming it isn’t dependent on any other data in our component).

We verify if the answer was OK within the first.then() callback (response.ok). If that’s the case, we’ll send our response to the next step, then call it back as JSON data, as that’s what our random user API will return.

If the response isn’t satisfactory, we’ll infer there was a problem with the request. We need to handle issues ourselves while using fetch, thus we toss the answer as an error for our code to handle.

  1. In React, how do you fetch data? Using the await / async syntax

Promises may now be resolved using the async / await syntax in ES7.

This has the advantage of removing our.then(),.catch(), and.finally() callbacks and just returning our asynchronously processed data as if we were writing synchronous code without promises.

In other words, while using async / await with React, we don’t need to rely on callbacks.

It’s important to remember that while using useEffect, the effect function (the first argument) cannot be made async.

As a consequence, rather than making that method asynchronous, we may construct a distinct async function in our component that we can call synchronously. That is, it is not preceded by the await keyword.

We develop an async method named getData in this example. We can get our data as expected by calling it synchronously within useEffect.

  1. Using the React Query Library to Fetch Data in React

Using custom hooks to obtain our data and all of its associated states is a terrific way to write much more compact HTTP queries. React Query, on the other hand, is a library that takes data fetching using hooks to the next level.

React Query not only offers us a lot of state management capabilities to control when, how, and how often our data is requested, but it also allows us to utilise custom hooks that we can simply reuse throughout our components.

React query, in particular, provides us with a cache, which you can see in the React Query Devtools below. This makes it simple to handle the requests we’ve made based on the key-value we supply for each request.

The string ‘random-user’ identifies our request for random user data in the queries below (provided as the first argument to useQuery).

We can perform powerful things like fetch, validate, and reset our different queries by referencing that key.

We will have to fetch data in react js every time our component is mounted if we utilise our custom hook solution or useEffect. In the vast majority of situations, this is unnecessary. We shouldn’t have to show the loading state every time we display our component if our external state hasn’t changed.

React Query substantially enhances our user experience by attempting to deliver data from its cache first, then updating the data in the background to reflect changes if our API status changes.

It also provides us with a set of strong capabilities to help us better manage our requests as our data evolves.

If our application permitted us to add a new user, for example, we may want to re-fetch that query after the user was added. We might wish to request that the query be refreshed every minute or so if we know the query is changing regularly. Alternatively, the user might be refreshed every time they concentrate on their window tab.

In a nutshell, React Query is the go-to solution for not just generating succinct requests, but also efficiently and effectively handling the data returned for our HTTP calls across all of our app’s components.