[Toast] - How to use toast outside of component?
See original GitHub issueThe chakra toasts are really beautiful, but unfortunately it seems like there is no way to use them outside of functional components!!
I want to invoke the toast on the success or failure of Rest API, which obviously is in .ts file and outside of component.
Any solution/workaround for this would be highly appreciated. Thanks!
Fo eg:
In my user component:
export const Users = () => {
.......
useEffect(() => {
dispatch(getUsers());
}, [dispatch]);
........
}
and then in my user action file :
export const getUsers = () => {
return (dispatch: Dispatch) => {
getUsersService()
.then((response: any) => {
dispatch(setUserList(response));
// i want to invoke success toast here
})
.catch((error: Error) => {
// i want to invoke error toast here
});
};
};
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Dispatch toast outside of react component | React-Toastify
The toast emitter is just a regular Javascript object, nothing less nothing more. You can dispatch notification from almost anywhere.
Read more >toast() API
Call it to create a toast from anywhere, even outside React. Make sure you add the <Toaster/> component to your app first.
Read more >Toast - Chakra UI
The toast component is used to give feedback to users after an action has taken ... Use createStandaloneToast to create toasts from outside...
Read more >How To create a Global Component like Toast in React And ...
I am trying to create a App using React. I want to add a toast component globally to the app so that it...
Read more >Building a toast component - web.dev
The toasts container does all the layout work for presenting toasts. It's fixed to the viewport and uses the logical property inset to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jmiazga I tried passing toast as a param, and it works fine!
code:
user action file (.ts file)
@ljosberinn , yeah your solution also looks great, but for my use case i would try to make it generic just by passing toast as shown above , as i have 30+ api integrations required with different success/error/warning toasts to be displayed - hence would be difficult to make it generic with custom hooks!
Thanks guys for your solutions. Much appreciated!