Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/content/reference/rsc/server-actions.md
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';
Copy link
Author

@IGassmann IGassmann Oct 2, 2024

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.


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';
Copy link
Author

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 imports the used React functions.

import { updateName } from './actions';

function UpdateName() {
const [name, setName] = useState('');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input wasn't using name since it's an uncontrolled input.

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('');
Copy link
Author

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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 formRef.current.reset();, I guess exactly because it is not reset by default? (thought seeing the other mistakes there, it might be?).

Copy link
Author

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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>}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state didn't exist.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this def was a LLM

Choose a reason for hiding this comment

The 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`);
Loading