DEV Community

MOHSIN ALI SOOMRO
MOHSIN ALI SOOMRO

Posted on • Edited on

5

Use react-hot-toast with Promise & Axios

Promise

A promise is an object that may produce a single value some time in the future : either a resolved value, or a reason that it's not resolved (e.g., a network error occurred). A promise may be in one of 3 possible states: fulfilled, rejected, or pending

👉 A promise can states

✅ fulfilled - The action relating to the promise succeeded
❌ rejected - The action relating to the promise failed
⚪ pending - Hasn't fulfilled or rejected yet

Axios

Axios is a Javascript library used to make HTTP requests from node. js or XMLHttpRequests from the browser and it supports the Promise API that is native to JS ES6. It can be used intercept HTTP requests and responses and enables client-side protection against XSRF. It also has the ability to cancel requests

react-hot-toast

Smoking hot Notifications for React.
Lightweight, customizable and beautiful by default.

React Code

import toast, { Toaster } from "react-hot-toast";
import { useEffect } from "react";
import axios from 'axios'
export default function App() {
  const fetchData = async () => {
    const response = await axios.get(
      "https://jsonplaceholder.typicode.com/users"
    );
    console.log({ response });
    return response;
  };

  useEffect(() => {
   const callFunction= fetchData();
   toast.promise(callFunction, {
    loading: "Process",
    error: "error occurs in data",
    success: "get data successfully...."
  });
  }, []);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <Toaster />
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

👏 Thanks for reading

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

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

Okay