reactjs / reactjs/react.dev

[Suggestion]: Provide examples for useActionState with onSubmit handlers

Đang mở
#7,310 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

type: documentation
Ngôn ngữ chính
JavaScript
Star
11.8k
Fork
7.9k
Merge trung bình
1 ngày 11 giờ
Pull request đã merge (30 ngày)
11

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với trang tham chiếu useActionState được liên kết trong issue và xem lại ví dụ form action hiện có. Thêm một ví dụ onSubmit cho các biểu mẫu được kiểm soát, gồm nhiều bước, giải thích cách gọi form action và xử lý kết quả của nó, sau đó xác minh rằng hành vi được ghi trong tài liệu giải quyết sự nhầm lẫn được báo cáo về await và TypeScript.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, react
Lĩnh vực
documentation, frontend
Loại issue
Tài liệu
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
55/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.