-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Fix server action code example #7202
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ When the bundler builds the `EmptyNote` Client Component, it will create a refer | |
|
||
```js [[1, 2, "createNoteAction"], [1, 5, "createNoteAction"], [1, 7, "createNoteAction"]] | ||
"use client"; | ||
import {createNoteAction} from './actions'; | ||
import { createNoteAction } from './actions'; | ||
|
||
function EmptyNote() { | ||
console.log(createNoteAction); | ||
|
@@ -103,32 +103,33 @@ export async function updateName(name) { | |
} | ||
``` | ||
|
||
```js [[1, 3, "updateName"], [1, 13, "updateName"], [2, 11, "submitAction"], [2, 23, "submitAction"]] | ||
```js [[1, 4, "updateName"], [1, 14, "updateName"], [2, 11, "submitAction"], [2, 24, "submitAction"]] | ||
"use client"; | ||
|
||
import {updateName} from './actions'; | ||
import { useState, useTransition } from 'react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of the code examples in the docs imports the used React functions. |
||
import { updateName } from './actions'; | ||
|
||
function UpdateName() { | ||
const [name, setName] = useState(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The input wasn't using |
||
const [error, setError] = useState(null); | ||
|
||
const [isPending, startTransition] = useTransition(); | ||
|
||
const submitAction = async () => { | ||
const submitAction = async (formData) => { | ||
startTransition(async () => { | ||
const {error} = await updateName(name); | ||
if (!error) { | ||
setError(error); | ||
const name = formData.get('name'); | ||
const result = await updateName(name); | ||
if (result?.error) { | ||
setError(result.error); | ||
} else { | ||
setName(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the form submission succeeds, React already automatically resets the form. No need to reset manually the input value. However, we need to reset the error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "When the form submission succeeds, React already automatically resets the form." sorry where's that claim coming from? In the official docs there's an example doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It comes from the docs as well. I believe the example you mention resets the form manually because it's demonstrating optimistic behavior. If it didn't reset manually, it would have to wait for the request to complete for React to reset the form. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah gotcha, thanks! I am just a passerby that was curious about the PR though, cannot review/approve/etc here, but looks good! |
||
setError(null); | ||
} | ||
}) | ||
} | ||
|
||
return ( | ||
<form action={submitAction}> | ||
<input type="text" name="name" disabled={isPending}/> | ||
{state.error && <span>Failed: {state.error}</span>} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this def was a LLM There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and a bad one at that |
||
{error && <span>Failed: {error}</span>} | ||
</form> | ||
) | ||
} | ||
|
@@ -148,7 +149,7 @@ You can pass a Server Action to a Form to automatically submit the form to the s | |
```js [[1, 3, "updateName"], [1, 7, "updateName"]] | ||
"use client"; | ||
|
||
import {updateName} from './actions'; | ||
import { updateName } from './actions'; | ||
|
||
function UpdateName() { | ||
return ( | ||
|
@@ -167,10 +168,11 @@ For more, see the docs for [Server Actions in Forms](/reference/rsc/use-server#s | |
|
||
You can compose Server Actions with `useActionState` for the common case where you just need access to the action pending state and last returned response: | ||
|
||
```js [[1, 3, "updateName"], [1, 6, "updateName"], [2, 6, "submitAction"], [2, 9, "submitAction"]] | ||
```js [[1, 4, "updateName"], [1, 7, "updateName"], [2, 7, "submitAction"], [2, 10, "submitAction"]] | ||
"use client"; | ||
|
||
import {updateName} from './actions'; | ||
import { useActionState } from 'react'; | ||
import { updateName } from './actions'; | ||
|
||
function UpdateName() { | ||
const [state, submitAction, isPending] = useActionState(updateName, {error: null}); | ||
|
@@ -192,10 +194,11 @@ For more, see the docs for [`useActionState`](/reference/react-dom/hooks/useForm | |
|
||
Server Actions also support progressive enhancement with the third argument of `useActionState`. | ||
|
||
```js [[1, 3, "updateName"], [1, 6, "updateName"], [2, 6, "/name/update"], [3, 6, "submitAction"], [3, 9, "submitAction"]] | ||
```js [[1, 4, "updateName"], [1, 7, "updateName"], [2, 7, "/name/update"], [3, 7, "submitAction"], [3, 10, "submitAction"]] | ||
"use client"; | ||
|
||
import {updateName} from './actions'; | ||
import { useActionState } from 'react'; | ||
import { updateName } from './actions'; | ||
|
||
function UpdateName() { | ||
const [, submitAction] = useActionState(updateName, null, `/name/update`); | ||
|
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.
Most of the code examples in the docs have spaces within the brackets.