[Bug]: Example given in "Handling return values in forms" section for "use-server" docs fails to compile
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- JavaScript
- Sterne
- 11.8k
- Forks
- 7.9k
- Ø Merge
- 1 T. 11 Std.
- Gemergte PRs (30 T.)
- 11
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem verlinkten Abschnitt „Handling return values in forms“ auf der use-server-Seite und vergleiche dessen useActionState-Beispiel mit der verlinkten useActionState-Dokumentation. Aktualisiere das requestUsername-Beispiel so, dass seine Argumente der dokumentierten Aktionssignatur entsprechen, und überprüfe anschließend, dass das Dokumentationsbeispiel kompiliert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, react
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 1/5
- Geschätzter Aufwand
- Unter einer Stunde
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 45/100