[Suggestion]: Provide examples for useActionState with onSubmit handlers
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- JavaScript
- Estrelas
- 11.8k
- Forks
- 7.9k
- Merge médio
- 1d 11h
- PRs com merge (30d)
- 11
Descrição
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.
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece pela página de referência de useActionState vinculada na issue e revise o exemplo existente de form action. Adicione um exemplo de onSubmit para formulários controlados e com várias etapas que explique como invocar a form action e lidar com seu resultado; em seguida, verifique se o comportamento documentado aborda a confusão relatada sobre await e TypeScript.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- javascript, react
- Domínio
- documentation, frontend
- Tipo de issue
- Documentação
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 55/100