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

Try useOptimistic with useFormState and progressive enhancement #38

Closed
wants to merge 6 commits into from
Closed
Changes from 3 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
24 changes: 22 additions & 2 deletions apps/shared-app/src/client/product.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,30 @@ export interface ProductProps {
}

export function Product({buy}: ProductProps): JSX.Element {
const [result, formAction] = ReactDOM.useFormState(buy, undefined);
const [formState, formAction] = ReactDOM.useFormState(buy, undefined);

const [result, setOptimisticResult] = React.useOptimistic<
BuyResult | undefined,
number
>(formState, (prevResult, quantity) => ({
status: `success`,
quantity,
totalQuantityInSession: prevResult?.totalQuantityInSession ?? 0 + quantity,
}));

return (
<form action={formAction}>
<form
action={formAction}
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);

React.startTransition(() => {
setOptimisticResult(parseInt(formData.get(`quantity`) as string, 10));
formAction(formData);

Choose a reason for hiding this comment

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

You should await this 😄

Choose a reason for hiding this comment

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

You need to use the hook version so that this transition is attached to the component, and then await the action. Otherwise things like errors from the action won't be surfaced.

Copy link
Owner Author

Choose a reason for hiding this comment

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

This can't be awaited, a formAction returns void, not Promise<void>.

Choose a reason for hiding this comment

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

Ah good point!

Copy link
Owner Author

Choose a reason for hiding this comment

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

You need to use the hook version so that this transition is attached to the component, and then await the action. Otherwise things like errors from the action won't be surfaced.

How can I verify this statement? Using React.startTransition, at least the component stack looks fine when forcing a network error:
Screenshot 2024-01-12 at 20 00 54

Copy link
Owner Author

Choose a reason for hiding this comment

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

@tom-sherman Sounds to me like the error behavior you've described here, which I couldn't reproduce, is addressed in facebook/react#28111. Am I interpreting this correctly?

});
}}
>
<p className="my-2">
This is a client component that renders a form with a form action. On
submit, a server action is called with the current form data, which in
Expand Down