[Suggestion]: Document that useFormStatus works with a preventDefault-ed onSubmit + startTransition
Maintainer thường phản hồi trong vòng 2 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- javascript, react
- Lĩnh vực
- documentation
Hướng nghiên cứu
Cập nhật trang tham khảo useFormStatus tại react.dev/reference/react-dom/hooks/useFormStatus. Đọc các phần Returns và Usage, sau đó kiểm tra PR React được liên kết và các trường hợp trong ReactDOMForm-test.js để xác nhận các điều kiện được mô tả. Bổ sung phần giải thích về trạng thái đang chờ và một ví dụ ngắn với onSubmit/preventDefault()/startTransition; hoàn tất khi cả hai lời gọi bắt buộc và hành vi được ghi lại chính xác.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
Document that useFormStatus also reports pending: true when a submit event handler calls event.preventDefault() and starts a Transition with startTransition, not only when the form uses the action prop.
Page
https://react.dev/reference/react-dom/hooks/useFormStatus
Details
I'm implementing a custom <Form> component that accepts a Transition-based prop like submitAction (it validates and transforms the values, then calls submitAction inside startTransition). I wanted to provide the form's pending state through useFormStatus itself, instead of adding a custom context. While investigating, I found that this already works, but because it is not documented, I could not tell whether it is safe to rely on.
The useFormStatus page currently describes the status only in terms of the action prop on the parent <form>. However, since https://github.com/facebook/react/pull/29019 (shipped in React 19.0.0), useFormStatus is also activated when startTransition is called inside a submit event handler that is preventDefault-ed:
After this change,
useFormStatuswill also be activated if you callstartTransition(actionFn)inside a submit event handler that ispreventDefault-ed.
The PR describes this as the way for UI component libraries to implement a custom action prop with onSubmit, and the behavior is covered by tests in ReactDOMForm-test.js:
- useFormStatus is activated if startTransition is called inside preventDefault-ed submit event
- useFormStatus is not activated if startTransition is not called
- useFormStatus is not activated if event is not preventDefault-ed
This matters for form components like mine that cannot use the action prop, for example because they validate and transform values before calling the action, or because they do not want the automatic form reset.
I confirmed the behavior with React 19.3.0 in Chrome.
Playground: https://codesandbox.io/p/sandbox/custom-useformstatus-fm2fkl
// App.js
import { startTransition, useActionState } from "react";
import { useFormStatus } from "react-dom";
// A fake request that takes 2 seconds.
function save(value) {
return new Promise((resolve) => setTimeout(() => resolve(value), 2000));
}
// A form component in a UI library. It does not use the `action` prop,
// e.g. because it validates and transforms the values before calling the action.
function MyForm({ submitAction, children }) {
return (
<form
onSubmit={(event) => {
event.preventDefault();
const value = new FormData(event.currentTarget).get("name");
startTransition(async () => {
await submitAction(value);
});
}}
>
{children}
</form>
);
}
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? "Sending" : "Send"}
</button>
);
}
function App() {
const [state, sendAction] = useActionState(async (prev, value) => {
await save(value);
return value;
}, null);
return (
<MyForm submitAction={sendAction}>
<input name="name" />
<SubmitButton />
{state && <p>Saved: {state}</p>}
</MyForm>
);
}
export default App;
pending stays true until the action finishes, and the form is not reset.
Suggested changes:
- In "Returns", say that
pendingis alsotruewhile a Transition started from apreventDefault-ed submit event of the parent<form>is pending. - In "Usage", add a short example of the
onSubmit+preventDefault()+startTransitionpattern, and note that bothpreventDefault()andstartTransitionin the same event are required.
- Ngôn ngữ chính
- JavaScript
- Star
- 11.8k
- Fork
- 8k
- Merge trung bình
- 2 ngày 31 phút
- Pull request đã merge (30 ngày)
- 8
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của reactjs/react.dev
-
[Suggestion]: lazy initialization example can encourage unsafe patterns - show exampleCó thể đã có người làm @Mahendra-2006 đã nhận 5 ngày trước. Đang mởtype: documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
reactjs/react.dev#8677 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
type: documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
reactjs/react.dev#8661 · 1 bình luận · 7 reaction ·
Maintainer thường phản hồi trong vòng 2 ngày
-
docs: improve useState explanation in quick startCó thể đã có người làm @tirthraj01 đã nhận 24 ngày trước. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 2 ngày
-
[Bug]: Next button throws a runtime error instead of cycling to the first sculptureCó thể đã có người làm @Saurav10codes đã nhận 32 ngày trước. Đang mởbug: unconfirmed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
reactjs/react.dev#8634 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[Typo]: Repeaded words in Pitfall sectionCó thể đã có người làm @mdimamhosen đã nhận 34 ngày trước. Đang mởtype: typos
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
reactjs/react.dev#8622 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của reactjs/react.dev
Issue tương tự
-
[DevEx]: Playground specs are excluded from both GUI and CI, so videoPlayground is unreachableĐang mởdev experience
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
cuttle-cards/cuttle#1413 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
macOS
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
beyondcode/herd-community#1761 ·
-
More wallpapersĐang mởenhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
-
Make a dry lesson funĐang mởcontent good first issue hacktoberfest
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
vieanderes/understory#19 ·
-
automated issue report
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
lirantal/discoprint#37 ·
Maintainer thường phản hồi trong vòng 1 ngày