[Suggestion]: Provide examples for useActionState with onSubmit handlers
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 11.8k
- 派生
- 7.9k
- 平均合并
- 1 天 11 小时
- 30 天内合并 PR
- 11
描述
Summary
Add examples to the useActionState documentation showcasing how it can work inside onSubmit handlers of forms.
Page
https://react.dev/reference/react/useActionState
Details
The docs show how to use useActionState with the action property of a form. However, the docs don't show how to use useActionState when you need to use it from onSubmit instead. This has been something that I have been fighting.
Let's say you are developing a multi-step / wizard form across multiple components. Not all of the form is rendered at the same time, so uncontrolled components won't work here. You need to use controlled components for your input fields in order to keep all the form state as you go through steps.
When it comes to submission, using action={formAction} won't work anymore, because that will only submit with the input fields currently rendered. It is not at all clear what the expectations are for doing this in the onSubmit handler instead.
When I attempt to do something like this:
<form
onSubmit={form.onSubmit(async (values, event) => {
const formData = new FormData();
for (const [key, value] of Object.entries(values)) {
formData.append(key, value);
}
startTransition(async () => {
await formAction(formData);
});
})}
>
I get interesting messages like TS80007: await has no effect on the type of this expression. on the line calling my form action, and I can't await the result and/or handle it.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中链接的 useActionState 参考页面开始,并查看其中现有的 form action 示例。为受控的多步骤表单添加一个 onSubmit 示例,说明如何调用 form action 及处理其结果,然后确认文档中描述的行为解决了所报告的关于 await 和 TypeScript 的困惑。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, react
- 领域
- documentation, frontend
- Issue 类型
- 文档
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 55/100