Comments (11)
Have you tried ensuring your .catch
was first and .then
following it? Weirdly enough, that fixed this same issue for me.
const callMyFunction = callFunction({ username: this.state.username })
.catch((error) => {
// Getting the Error details.
console.error(error.message);
return error.message;
})
.then((res) => {
console.log(res);
refreshData();
});
toast.promise(callMyFunction, {
loading: "Loading...",
success: "Everything went smoothly.",
error: "Uh oh, there was an error!",
});
(I know this isn't an Axios example, but the promise structure is relatively the same)
from react-hot-toast.
This worked for me
const res = fetch(apiUrl);
toast.promise(res, {
loading: 'Loading ...',
success: (data) => {
console.log(data);
if (data.status === 500) throw new Error('server error');
return 'Everything went smoothly.';
},
error: 'Uh oh, there was an error!',
});
from react-hot-toast.
The toast.promise function returns the promise, so you could use something like this:
const callMyFunction = toast.promise(callFunction({ username: this.state.username }), {
loading: "Loading...",
success: "Everything went smoothly.",
error: "Uh oh, there was an error!",
});
callMyFunction.then((res) => {
console.log(res);
refreshData();
})
.catch((error) => {
// Getting the Error details.
console.error(error.message);
return error.message;
});
from react-hot-toast.
In case anyone using firebase:
toast.promise( createUserWithEmailAndPassword(authInstance, email, password), { loading: "loading...", success: (result) => `success, ${result.user}`, error: (e) => `error ${e.message}`, } );
Thanks a lot, this work for me...
from react-hot-toast.
Hey! I'm not entirely sure what is going on but I think it should work like this:
const promise = axios.patch(url, params)
toast.promise(promise, {
loading: 'Saving...',
success: <b>saved!</b>,
error: <b>Could not apply.</b>
})
const result = await promise
from react-hot-toast.
Hey! I'm not entirely sure what is going on but I think it should work like this:
const promise = axios.patch(url, params) toast.promise(promise, { loading: 'Saving...', success: <b>saved!</b>, error: <b>Could not apply.</b> }) const result = await promise
It is still popping up as a success when I try your way even when the network call is failing with a 404:
export const postVersionBulkAction = (args) => {
return async (dispatch) => {
const promise = axios.patch(badUrl, params )
toast.promise(promise, {
loading: 'Saving...',
success: 'saved',
error: 'error'
})
const result = await promise
}
}
it pops up a success immediately
Edited: removed some of the extraneous stuff from the function to make it easier for someone to diagnose
from react-hot-toast.
@JaredDahlke did you ever resolve this? I am also seeing this error when trying to write a unit test for a component using toast.promise
from react-hot-toast.
Here is what works for
const promise = axios.patch(url, params)
toast.promise(promise, {
loading: 'Saving...',
success: <b>saved!</b>,
error: <b>Could not apply.</b>
},
, {
style: {
minWidth: '250px',
}
}
)
.then((r) => r)
.catch((error) => error);
from react-hot-toast.
In case anyone using firebase:
toast.promise(
createUserWithEmailAndPassword(authInstance, email, password),
{
loading: "loading...",
success: (result) => `success, ${result.user}`,
error: (e) => `error ${e.message}`,
}
);
from react-hot-toast.
In my case, make sure you throw an error after doing a catch
This Work for me
const callMyFunction = callFunction({ username: this.state.username })
.catch((error) => {
// Getting the Error details.
console.error(error.message);
throw error; // throw again after you catch
})
.then((res) => {
console.log(res);
refreshData();
});
toast.promise(callMyFunction, {
loading: "Loading...",
success: "Everything went smoothly.",
error: "Uh oh, there was an error!",
});
from react-hot-toast.
This is a bit tricky due to the next:
- The server errors are not handled as an error within javascript
- We expect errors from the server as normal error
I solved this by doing the next: throwing an error from the my toast code if the server was not sending the expected/ok statues.
toast.promise(fetch(someURL), {
loading: "Loading ...",
success: data => {
if (!data.ok) { //here you can that this will throw the error from the returned data. Usually it's treated as normal thing.
throw new Error(`Statues code ${data.status}`);
}
return "Everything went good";
},
error: e => {
return `Uh oh, there was an error! ${e.message}`;
},
})
from react-hot-toast.
Related Issues (20)
- Icons not showing in remix in production HOT 8
- streamline ToastOption types with code logic HOT 1
- Minor style issue when using Google Translate plugin on site
- Not able to use this package with npm version 8.19.4?
- Toaster is not working in Next.js 13.5.3 HOT 1
- Type error with figma widget jsx element
- dismiss method does not close all custom toasts
- Custom / Position HOT 1
- Property 'document' doesn't exist || Cannot read property 'useToaster' of undefined. React Native HOT 2
- Toaster gives Unhandled RunTime Error on Nextjs HOT 5
- Toasts not firing (help). HOT 2
- How can i pass dynamic variable to toast.promise loading? HOT 1
- Allow customizing slide up/down animation
- Allow customizing exit animation when user manually dismisses toast HOT 2
- toast.loading() not working with nextjs 14 HOT 3
- Pause on Hover does not work with Headless UI HOT 1
- Dismiss toast by matching toastIds with regex
- Custom output direction HOT 1
- Support loading with "mask"
- Element type Error when using with nextjs and redux-toolkit
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-hot-toast.