Skip to content

Commit 947c926

Browse files
committed
update code example for Handling multiple submission types
1 parent 4f39d4b commit 947c926

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

  • src/content/reference/react-dom/components

src/content/reference/react-dom/components/form.md

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -461,47 +461,58 @@ A form can have more than one submit button, each running a different action. Se
461461
462462
When a button without `formAction` submits the form, React calls the form's `action`. When a button with `formAction` submits the form, React calls that button's action instead. For example, the form below publishes an article by default, but its **Save draft** button stores the current content without publishing it.
463463
464-
In this example the draft is held in state, so the saved content stays in the textarea after you submit it. In a real app you would persist the draft on the server. Pass a [Server Function](/reference/rsc/server-functions) (a function marked with [`'use server'`](/reference/rsc/use-server)) to `formAction` to save the draft from the server, optionally combined with [`useActionState`](/reference/react/useActionState) to track its pending state and result.
465-
466464
<Sandpack>
467465
468466
```js src/App.js
469-
import { useState } from 'react';
467+
import { useActionState } from 'react';
470468

471469
export default function ArticleForm() {
472-
const [draft, setDraft] = useState(() => new FormData());
470+
// Hold the saved draft in state so the textarea keeps its content after saving
471+
const [formState, dispatchFormState] = useActionState((state, payload) => {
472+
const content = payload.data.get('content');
473+
switch (payload.type) {
474+
case 'save':
475+
476+
alert(`Your draft of '${content}' was saved!`);
477+
// Keep the submitted content as the current draft
478+
return payload.data;
479+
case 'publish':
480+
alert(`'${content}' was published!`);
481+
// reset the form
482+
return new FormData();
483+
}
484+
});
473485

474486
function publish(formData) {
475-
const content = formData.get('content');
476-
alert(`'${content}' was published!`);
477-
// Clear the saved draft after publishing.
478-
setDraft(new FormData());
487+
dispatchFormState({
488+
type: 'publish',
489+
data: formData
490+
})
479491
}
480492

481493
function save(formData) {
482-
const content = formData.get('content');
483-
alert(`Your draft of '${content}' was saved!`);
484-
// Keep the submitted content as the current draft.
485-
setDraft(formData);
494+
dispatchFormState({
495+
type: 'save',
496+
data: formData
497+
})
486498
}
487499

488-
const savedContent = draft.get('content') || '';
500+
// const savedContent = draft.get('content') || '';
489501
return (
490502
<form action={publish}>
491503
<textarea
492-
// Changing the key resets the textarea to the saved draft.
493-
key={`${savedContent}-content`}
494504
name="content"
495505
rows={4}
496506
cols={40}
497-
defaultValue={savedContent}
507+
defaultValue={formState?.get('content') || ""}
498508
/>
499509
<br />
500510
<button type="submit" name="button" value="submit">Publish</button>
501511
<button formAction={save}>Save draft</button>
502512
</form>
503513
);
504514
}
515+
505516
```
506517
507518
</Sandpack>

0 commit comments

Comments
 (0)