DEV Community

Cover image for useAxios - React custom http client
Nelson Odo
Nelson Odo

Posted on

3 2

useAxios - React custom http client

Often times we are faced with difficulty in tracking the request status while making api calls on our react app. Request status here could be the data response, loading status, error encountered, etc.
The work around is usually to create individual useState to manage all the status that we want (like error, data, etc).

This hook is built on top of javascript axios package

First, let’s create the useAxios custom hooks

//useAxios.js

import { useState, useEffect } from "react";
import axios from "axios";

const useAxios = props => {
  const [axiosData, setAxiosData] = useState({
    url: null,
    method: "GET",
    body: null,
    headers: null
  });
  const [data, setData] = useState(null);
  const [error, setError] = useState(null);
  const [loading, setLoading] = useState(false);
  const [status, setStatus] = useState(null);

  const sourceToken = axios.CancelToken.source();
  const fetchApi = async () => {
    setLoading(true);
    setData(null);
    setError(null);
    setStatus(null);
    try {
      const data = axiosData?.body ? { data: axiosData?.body } : {};
      const headers = axiosData?.headers ? { headers: axiosData?.headers } : {};
      const response = await axiosClient({
        url: axiosData.url,
        method: axiosData.method ?? "GET",
        cancelToken: sourceToken.token,
        ...data,
        ...headers,
      });
      setLoading(false);
      setStatus(response?.status);
      setData(response?.data);
      if (props.onSuccess) props.onSuccess(response?.data);
    } catch (error) {
      const errorMsg = error?.message;
      setLoading(false);
      setError(String(errorMsg));
      setStatus(error?.response?.status);
      if (props.onError) props.onError(errorMsg);
    }
  };

  useEffect(() => {
    if (axiosData.url != null) {
      fetchApi();
    }
    // abort the fetch if screen is dead/inactive
    return () => sourceToken.cancel();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [axiosData]);

  // return [setPostData, data, error, loading, status];
  return { axios: setAxiosData, data, error, loading, status };
};

export default useAxios;
Enter fullscreen mode Exit fullscreen mode

Initialize request with useAxios

To initialize your request

//index.js

import useAxios from "customHooks/useAxios";
const { axios, data, error, loading, status } = useAxios();
Enter fullscreen mode Exit fullscreen mode

axios: custum axios function to make the api request
data: reponse data from the endpoint
error: contains error message if an error occures
status: returns status code of the request

To trigger the api request, for instance when a button is clicked

const onClick = ()=>{
  axios({
      url: "https://example.com"
    });
}
Enter fullscreen mode Exit fullscreen mode

This makes a get request to https://example.com
The axios function can accept multiple parameters like method, body, and headers

What about POST/PATCH/DELETE requests

To make a request with a custom methods, you could do

const onClick = ()=>{
    const data = {
      name:"Sample name",
      category:"sampleCat",
    }
    axios({
      url: "https://example.com",
      method:"POST",
      body:data,
      headers:{...}
    });
}
Enter fullscreen mode Exit fullscreen mode

You can also also listen for success or error response while initializing the useAxios. Here is how you can do this,

 const { axios, data, error, loading, status } = useAxios({
    onSuccess: (data) => {
      console.log(data);
    },
    onError: (error) => {
      console.log(error);
    }
  });
Enter fullscreen mode Exit fullscreen mode

onSuccess is fired when the data is returned from the endpoint
onError is fired when an error occurs.

You can as well enhance the useAxios hook to include base url, token, etc...

Thanks for reading!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

πŸ‘₯ Ideal for solo developers, teams, and cross-company projects

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay