-
Notifications
You must be signed in to change notification settings - Fork 29.5k
Description
What is the improvement or update you wish to see?
Since the docs: useFormState PR was merged, the Documentation around Forms and Mutations no longer provides any example on top of which you can build Toast/Snackbar notifications.
The PR removed the following Code from the Documentation:
'use client'
import { create } from './actions'
import { useState } from 'react'
export default function Page() {
const [message, setMessage] = useState<string>('')
async function onCreate(formData: FormData) {
const res = await create(formData)
setMessage(res.message)
}
return (
<form action={onCreate}>
<input type="text" name="item" />
<button type="submit">Add</button>
<p>{message}</p>
</form>
)
}
However, wrapping a Server Action in a Client Action and then adjusting React state in that Client Action seems like a Core principal of how Server Actions can be used. So, I think that at least one example showcasing this should be in the Next.js documentation. This comes of course with the tradeoff that Progressive Enhancement does not work, but it is an important principal for interactivity.
@leerob I tried to build Toast/Snackbar Notifications with useFormState
as you suggested, but useFormState
is lacking an important concept that allows to close these notification Client-side. This is described below in detail.
Is there any context that might help us understand?
Before the docs: useFormState PR was merged, the recommended pattern in the docs for showing error and success states for Server Actions was as follows:
'use client'
import { create } from './actions'
import { useState } from 'react'
export default function Page() {
const [message, setMessage] = useState<string>('')
async function onCreate(formData: FormData) {
const res = await create(formData)
setMessage(res.message)
}
return (
<form action={onCreate}>
<input type="text" name="item" />
<button type="submit">Add</button>
<p>{message}</p>
</form>
)
}
With that approach, it was nicely possible to implement Toast/Snackbar notifications, because you could easilty reset React state and close these notifications via Client-side click handlers or timeouts.
The Scenario was as follows:
- A Toast/Snackbar notification is shown by adjusting React state inside a Client Action after the Server Action completes.
- When the user clicks on a Cancel-Icon inside that Toast/Snackbar notification you can easily adjust the React state again, to close that Notification. (with a solely Client-side event handler)
The new useFormState
approach:
With docs: useFormState the above approach was removed in favor of useFormState
. However, with useFormState
it does currently not seem possible to implement Toast/Snackbar notifications, as you do not have the possibility to reset the state Client-side. (e.g. via a Client-side onClick event on a Cancel-Icon inside the Toast/Snackbar)
Scenario for Implementing Toast/Snackbar notifications with useFormState
:
- A Toast notification is opened when the
state
ofuseFormState
becomes a certain value by executing theformAction
. (This part is covered by the docs and should work fine.) - When the user clicks on a Cancel-Icon inside that Toast Notification the state needs to be reset without executing the
formAction
or any other Server-side code. -> This part does not work withuseFormState
.
Does the docs page already exist? Please link to it.
https://nextjs.org/docs/app/building-your-application/data-fetching/forms-and-mutations