36
loading...
This website collects cookies to deliver better user experience
Before you go through this blog be familiar with React library and Application Programming Interface (API).
import {useEffect} from "react";
const fetchUsers = () => {
// Where we're fetching data from
return fetch("http://www.abc.cd/test")
// We get the API response and receive data in JSON format
.then((response) => response.json())
.then((data) => console.log(data))
.catch ((error) => console.error(error));}
response.json()
method.json()
method is to get the response object which is stored in data and used to update the state of users in our application..catch()
method. Any error encountered is used as a value to update our error’s state. useEffect()
hook with an empty dependencies array as the second argument so that our request is only made once, not dependent on any other data.useEffect()
hook:import {useEffect} from "react";
useEffect(() => {
fetchData()
}, []);
npm install axios
import axios from "axios"
const fetchData = () => {
return axios.get("http://www.abc.cd/test")
.then((response) => console.log(response.data));
}
Check out the official documentation for better knowledge.
async function fetchData() {
try {
const result = await axios.get("http://www.abc.cd/test")
console.log(result.data));
} catch (error) {
console.error(error);
}
}
When we use useEffect()
, the effect function (the first argument) cannot be made an async function. For that, we can create a separate async function in our component, which we can call synchronously within useEffect and fetch our data accordingly.
useFetch
. This hook accepts two arguments, the URL we need to query to fetch the data and an object representing the options we want to apply to the request.useEffect()
hook. We are going to use the Fetch API to make our request. For that, we have to pass the URL and the options we want to retrieve. From there, we get an object that we can use to render our application.import { useState, useEffect } from 'react';
const useFetch = (url = 'http://www.abc.cd/test', options = null) => {
const [data, setData] = useState(null);
useEffect(() => {
fetch(url, options)
.then(res => res.json())
.then(data => setData(data));
}, [url, options]);
return {data}
}
export default useFetch;
import useFetch from './useFetch';
const { data } = useFetch( 'http://www.abc.cd/test' );
console.log(data);
"It makes fetching, caching, synchronizing, and updating server state in your React applications a breeze”
npm install react-query react-query-devtools
Note: React Query also has its own dev tools which help us to visualize the inner workings of React Query.
import React from "react";
import ReactDOM from "react-dom";
import { QueryClient, QueryClientProvider, useQuery } from "react-query";
import { ReactQueryDevtools } from "react-query/devtools";
const queryClient = new QueryClient();
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<FetchData />
</QueryClientProvider>
);
}
function FetchData() {
const { data } = useQuery("UserData", () =>
fetch("http://www.abc.cd/test").then((res) => res.json())
);
return (
<div>
// data you want to show
<ReactQueryDevtools initialIsOpen />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
useState()
and useEffect()
hooks and replaces them with a few lines of React Query logic.For better insights check out the official documentation.