question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Toast] - How to use toast outside of component?

See original GitHub issue

The 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
fakfa19commented, May 18, 2022
import { createStandaloneToast } from "@chakra-ui/react";

const toast = createStandaloneToast();

toast({ title: "Toast", status: "success" });
5reactions
ganeshkrishnasharmacommented, Aug 16, 2020

@jmiazga I tried passing toast as a param, and it works fine!

code:

export const Users = () => {
   const toast = useToast();
   .......

    useEffect(() => {
      dispatch(getUsers(toast));
    }, [dispatch]);

   ........
 }

user action file (.ts file)

export const getUsers = (toast: any) => {
    return (dispatch: Dispatch) => {
       getUsersService()
       .then((response: any) => {
         dispatch(setUserList(response));
         toast({
          title: 'Users fetched successfully!',
          status: 'success',
        });
       })
      .catch((error: Error) => {
        toast({
          title: 'Failed to fetch users!',
          status: 'error',
        });
      });
  };
};

@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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found