-
Notifications
You must be signed in to change notification settings - Fork 27k
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
docs: useFormState
#55564
docs: useFormState
#55564
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Orca Security Scan Summary
Status | Check | Issues by priority | |
---|---|---|---|
Passed | Secrets | 0 0 0 0 | View in Orca |
} | ||
|
||
export function DeleteForm({ id }: { id: number }) { | ||
const [state, formAction] = useFormState(deleteTodo, initialState) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun fact: You can also do deleteTodo.bind(null, id)
either here or on the returned formAction. To pass the ID as an extra argument. It should preserve its ability to operate with progressive enhancement.
Gives you the same capabilities as closures but on the client. Using an actual closure on the client breaks progressive enhancement since now there's some client code running before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which pattern would you prefer for us to recommend? I can update this if you feel .bind()
is better.
docs/02-app/01-building-your-application/02-data-fetching/03-forms-and-mutations.mdx
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌
Co-authored-by: John Pham <johnphammail@gmail.com>
This PR leads to below pattern for setting error and success state being removed from the docs in favor of the new '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, unlike the pattern in the current docs, With the pattern that is currently present in the docs (which would be removed by this PR), implementing Toast/Snackbar Notifications works fine. So maybe it is better to keep this pattern in the docs as well and recommend it, if resetting state on the client side (without calling a Server Action) is desired (as it is for Toast/Snackbar Notifications). |
@schimi-dev this approach should still work for handling toasts, did you try it out? |
@leerob Thanks for your quick answer. The approach in the code snippet provided above works fine. But when I was checking your commits I saw that it was replaced/removed from the examples in favor of the However, I think it might help people if it was kept in the docs as a reference, to show that this works as well. So, maybe you could keep both approaches in the docs, the one from the code snippet and the new one describing the usage of |
|
Shipping so we can start getting more feedback – can add additional tweaks in follow up to the example if we want. Let me know. |
@leerob Thanks, I will play around with Scenario:
|
While I really see the benefit of the I am currently working on migrating some pages to the app-structure, and I was missing this example today. Glad I found this PR so I know I didn't make this functionality up ;-) I also think it would be great to provide a (simple) example that doesn't work with a |
This PR shows how to use a new React hook
useFormState
in the context of the Forms and Mutations docs. It also updates the forms example (next-forms
) to show the recommended patterns for loading / error states.Related: #55399
Success
CleanShot.2023-09-18.at.22.39.58.mp4
Loading / Error States
CleanShot.2023-09-18.at.22.41.57.mp4