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

[Bug]: Example given in "Handling return values in forms" section for "use-server" docs fails to compile #7167

Open
abraj opened this issue Sep 16, 2024 · 0 comments

Comments

@abraj
Copy link

abraj commented Sep 16, 2024

Summary

The following example is given in Handling return values in forms section for "use server" docs, fails to compile.

// requestUsername.js
'use server';

export default async function requestUsername(formData) {
  const username = formData.get('username');
  // ..
}
// UsernameForm.js
'use client';

import { useActionState } from 'react';
import requestUsername from './requestUsername';

function UsernameForm() {
  const [state, action] = useActionState(requestUsername, null, 'n/a');
  // ..
}

Page

https://react.dev/reference/rsc/use-server#handling-return-values

Details

It fails to compile because, as per the useActionState docs,

useActionState(action, initialState, permalink?)

the "action" (requestUsername in this case) should have previousState as the first argument,
and formData as the second argument.

However, for the above example, the requestUsername action has formData as the first argument,
rather than previousState. Therefore, it fails to compile.

The following is the correct way mentioned in the useActionState docs:

async function increment(previousState, formData) {
  return previousState + 1;
}

function StatefulForm({}) {
  const [state, formAction] = useActionState(increment, 0);
  // ..
}

I can raise a PR for the fix, if it's okay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant