DEV Community

Rafi
Rafi

Posted on

7 2

Simple hook to deal with async function calls

For smaller react projects I wanted something simple to deal with asynchronous function calls like fetch API. Rather than littering the component with useState for dealing with loader, error and response I wrote a really simple hook

import { useState, useCallback } from 'react';

function useAsync(handleAsync, initialResponse = {}) {
  const [isLoading, setLoading] = useState(false);
  const [error, setError] = useState(false);
  const [response, setResponse] = useState(initialResponse);

  const fetchValue = useCallback(
    async (options) => {
      setLoading(true);
      setError(false);

      try {
        const apiResponse = await handleAsync(options);
        setResponse(apiResponse);
        setLoading(false);
      } catch (error) {
        setError(true);
        setLoading(false);
        setResponse(error);
      }
    },
    [setError, setLoading, setResponse, handleAsync]
  );

  return {
    response,
    isLoading,
    error,
    fetchValue,
  };
}

export default useAsync;

So I can use this as follows

const { response, fetchValue, isLoading, error } = useAsync(signupUser);

const handleSubmit = useCallback(() => {
  fetchValue({ email, password });
}, [email, password, fetchValue]);
export const signupUser = async ({ email, password }) => {
  const response = await fetch(`dev.to/signup`, {
    credentials: "include",
    method: "POST",
    headers: {
      "Content-type": "application/json",
    },
    body: JSON.stringify({
      email,
      password,
    }),
  });

  if (!response.ok) {
    throw new Error(response);
  }

  return response.json();
};

react-async does provide something similar but it had lot of other functionality that I won't need. So going with a simple hook instead of a complete library just for this alone seemed like a better choice for me.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (4)

Collapse
 
rafi993 profile image
Rafi • Edited

The initialResponse is just bad naming. I should have named it something else (but I could not think of anything at the moment). It is just the initial value I want the response state to have.

Collapse
 
rafi993 profile image
Rafi • Edited

all of this could also be tracked in a single state too. But I just separated it just to be explicit. Thanks for your feed back :)

Collapse
 
instinct profile image
Instinct

Hey, need some help!

I have basic knowledge of react but I want to dive deeper. Can you suggest me a book where I can learn advanced react?

Collapse
 
rafi993 profile image
Rafi

If you know basic react and want to dive deeper I would suggest you to read react docs. It is really well organized. And build things with react as you learn it.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more