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

toast.loading() not working with nextjs 14 #324

Open
MauricioKruijer opened this issue Nov 1, 2023 · 3 comments
Open

toast.loading() not working with nextjs 14 #324

MauricioKruijer opened this issue Nov 1, 2023 · 3 comments

Comments

@MauricioKruijer
Copy link

Hi there, thanks for releasing this amazing product!

I would like to use it in my nextjs project and I'm running into a few issues with toast.promise() and toast.loading().
I am trying to disable a submit button -> show loading toast -> await my action -> show success (or error). Which would be the expected behaviour, however, I only get the loading toast only the second time I click the button. The success toast is more consistent.

Please have look at MauricioKruijer/nextjs-react-hot-toast@fb8db5a for more information. You can run this project to experience what I'm talking about.

There is an extract of my form that is trying to render the toast by using useFormState()

'use client'

import Button from "@/components/Button";
import {useFormState} from "react-dom";
import toast from "react-hot-toast";

function doStuff(formData: FormData) {
  // if (Math.floor((Math.random() * 4) + 1) === 1) {
  //   throw new Error('This is a planned error')
  // }
  return new Promise(resolve => setTimeout(resolve, 1500))
}

async function someAction(state: any, formData: FormData) {
  const toastId = toast.loading('Loading') // This toast is not not shown, sometimes on third or second time of hitting the button

  try {
    await doStuff(formData)
  } catch (error) {
    toast.dismiss(toastId);
    toast.error((error as Error).message)
    return {
      errors: 'something went wrong'
    }
  }

  toast.success('Success!', {id: toastId})

  return {
    message: 'ok'
  }
}

const Form = () => {
  const initialState = { message: null, error: null };
  const [state, dispatch] = useFormState(someAction, initialState);

  return (
    <form action={dispatch}>
      <Button />
    </form>
  )
}

export default Form
@tszhong0411
Copy link

tszhong0411 commented Jan 21, 2024

It seems that this project has been abandoned. Here is a minimal reproduction that there is a difference between triggering by click and form action when using react-hot-toast.

I tested that this is a problem only when using react-hot-toast.

https://codesandbox.io/p/devbox/reac-hot-toast-test-lz39vl?file=/app/page.tsx

@designbyadrian
Copy link

It seems that this project has been abandoned.

Not abandoned: #298

@bigblobby
Copy link

Could you use onSubmit instead? This looks to be working.

As far as i'm aware you'd usually only use server actions in nextjs, i don't see why you'd need client actions.

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

4 participants