Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1K
Visualizações
How to use react-toastify promise in axios

// how can I use the promise of toastify like I want to show spinner while fetching data then message success or failed

// but I am getting error in bellow code

const fetch = () => {
    axios
      .get("https://restcountries.com/v2/name/india")
      .then((res) => {
        toast.promise({
          pending:"pending",
          success:"success",
          error:"rejected"
        } )
        console.log(res);
      })
      .catch((err) => {
        toast.error("🦄 failed", {
          position: "top-center",
          autoClose: 2000,
          hideProgressBar: true,
          closeOnClick: true,
          pauseOnHover: true,
          draggable: true,
          progress: undefined
        });
      });
  };
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

According to toast API https://fkhadra.github.io/react-toastify/promise/ the syntax should be

const myPromise = fetchData();


toast.promise(myPromise, {
   loading: 'Loading',
   success: 'Got the data',
   error: 'Error when fetching',
})

An example which can be found on https://codesandbox.io/s/twilight-bash-jzs24y?file=/src/App.js

 export default function App() {
        
        const myPromise = new Promise((resolve) =>
         fetch("https://jsonplaceholder.typicode.com/todos/1")
           .then((response) => response.json())
           .then((json) => setTimeout(() => resolve(json), 3000)) 
           // setTimeout just for the example , cause it will load quickly without it .
        );

       useEffect(() => {
         toast.promise(myPromise, {
              pending: "Promise is pending",
              success: "Promise  Loaded",
              error: "error"
         });
       }, []);

       return (
             <div className="App">
                  <ToastContainer />
             </div>
       );
   }
about 4 years ago · Juan Pablo Isaza Relatório

0

If you are not using promise. Use toast.loading. (DOCS: https://fkhadra.github.io/react-toastify/promise/#toastloading)

const getData = () => {
 const id = toast.loading("Please wait...")
 axios.get(`some-url`)
   .then(res => { 
      toast.update(id, {render: "All is good", type: "success", isLoading: false});
 }).catch(err => {
        toast.update(id, {render: "Something went wrong", type: "error", isLoading: false });
   });
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda