Fetching data using the React Query in React
React Query is an npm package library created for data fetching purposes with a ton of functionalities loaded with it. In React Query, the state management, pre-fetching of data, request retries, and caching are handled out of the box. React Query is a critical component of the React ecosystem with over a million downloads weekly.
Let’s refactor the code snippet we used in the Fetching data using Axios section and experience the awesomeness of React Query:
- First, install React Query. In the root directory of the project, do the following:
npm install react-query
- Inside
App.js
, add the following:import {useQuery} from 'react-query'import axios from 'axios';function App() { const{data, isLoading, error} = useQuery( "speakers", ()=>{ axios( "https://jsonplaceholder.typicode.com/users")&...