Fetching data in React using XMLHttpRequest and Fetch API is fine but require a lot of boilerplate codes. Using a data fetching library is preferred as following solutions will simplify the logic of data fetching in your project with tons of amazing built-in features.
Axios (99k ⭐) -> A promise-based isomorphic HTTP client which can run in the browser and NodeJS with the same codebase. On the server-side it uses the native NodeJS http module, while on the browser it uses XMLHttpRequests.
React Query (33k ⭐) -> A library handles fetching, caching, background updates and stale data out of the box with zero-configuration. It is backend agnostic so you can use React Query with literally any asynchronous data fetching client.
SWR (26k ⭐) -> A React Hooks library for data fetching, created by Next.js team, lightweight, and backend agnostic.
Relay (17k ⭐) -> The production-ready GraphQL client for React, developed by Facebook, was designed to be performant from the ground up, built upon locally declaring data dependencies for components.
urql (8k ⭐) -> A highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
RTK Query (9k ⭐) -> An optional addon included in the Redux Toolkit package, and its functionality is built on top of the other APIs in Redux Toolkit.
Web applications normally need to fetch data from a server in order to display it. They also usually need to make updates to that data, send those updates to the server, and keep the cached data on the client in sync with the data on the server. This is made more complicated by the need to implement other behaviors used in today’s applications: