ui icon indicating copy to clipboard operation
ui copied to clipboard

Show a toast without using useToast hook

Open priyanthabuddhika opened this issue 1 year ago • 8 comments

Is there a way to show a toast using a method instead of the useToast hook ? It would be helpful to show toast messages outside of react components.

priyanthabuddhika avatar May 01 '23 19:05 priyanthabuddhika

No not possible (Assuming by "outside react components" you mean server component). The "useToast" hook does all the heavy lifting for the component, and has a bunch of things that wouldn't work in a server component like "useState", "useEffect" and event listeners.

puneet-sarhali avatar May 03 '23 20:05 puneet-sarhali

@puneet-sarhali Sorry I could have added an example before. No, Actually I didn't mean server components, what I meant was how to show a toast inside something like a interceptor in axios, in redux state actions which are not React jsx components.

priyanthabuddhika avatar May 04 '23 08:05 priyanthabuddhika

You can use toast function that we export to the file use-toast.ts

Natchii59 avatar May 07 '23 08:05 Natchii59

You can use toast function that we export to the file use-toast.ts

Can you describe why/when we should use the hook vs the function and vice versa?

joepetrillo avatar Jun 19 '23 22:06 joepetrillo

You can use toast function that we export to the file use-toast.ts

Can you describe why/when we should use the hook vs the function and vice versa?

For example, when you want to add an interceptor error for axios response. Some people create an api.ts file to configure an Axios instance, so you can't use Hooks in .ts file.

https://react.dev/warnings/invalid-hook-call-warning

benomzn avatar Sep 15 '23 04:09 benomzn

You can use toast function that we export to the file use-toast.ts

Can you describe why/when we should use the hook vs the function and vice versa?

For example, when you want to add an interceptor error for axios response. Some people create an api.ts file to configure an Axios instance, so you can't use Hooks in .ts file.

https://react.dev/warnings/invalid-hook-call-warning

same scenes.

carlos19960601 avatar Sep 22 '23 10:09 carlos19960601

any news on this? it'd be really useful in order to extract some logic out of the component

peppemig avatar Nov 11 '23 15:11 peppemig

import { toast } from "@/components/ui/use-toast"

robZuk avatar Jan 10 '24 16:01 robZuk