Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i pass dynamic variable to toast.promise loading? #321

Open
klmntv opened this issue Oct 30, 2023 · 1 comment
Open

How can i pass dynamic variable to toast.promise loading? #321

klmntv opened this issue Oct 30, 2023 · 1 comment

Comments

@klmntv
Copy link

klmntv commented Oct 30, 2023

something like this

toast.promise(
  myPromise,
  {
    loading: loadingProgress, // from useState hook for example
    success: 'Success',
    error: 'Error',
  },
)
@nakamuraos
Copy link

My solution is to use useRef.

This is implement example:

import { useRef } from "react";

const MyComponent = (props: any) => {
  const textRef = useRef<HTMLSpanElement>(null);

  const updateText = (text: string) => {
    if (textRef?.current) textRef.current.innerText = text;
  };

  const onHandle = async () => {
    updateText("Initiating...");
    await delay(3000);

    updateText("Optimizing images...");
    await delay(3000);

    updateText("Saving...");
  };

  const onClick = () => {
    toast.promise(onHandle(), {
      loading: <span ref={textRef}>Saving...</span>,
      success: "Successfully!",
      error: (error) => {
        console.log(error);
        return "Something were wrong.";
      },
    });
  };

  return (
    <div>
      <button onClick={onClick}>Show me</button>
    </div>
  );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants