Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

React `useForm`: add `onSuccess` / `onError` callbacks

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

Maintainer thường phản hồi trong vòng 1 ngày

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

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
67/100
Loại issue
Tính năng
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
react, typescript
Lĩnh vực
frontend

Hướng nghiên cứu

Start by reading the useForm implementation in use-form.ts and find where wasSuccessful, errors, and error are set inside the action. Check how the form's state and submission lifecycle are handled, then add callbacks through the options described in the issue. Done means success and error callbacks run for the corresponding outcomes while the existing typed form state remains available.

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

Mô tả

Version

void / @void/react 1.1.12

Problem

In React, form.post / put / patch / delete are form actions built on useActionState (#141), so they can't be awaited. That leaves no direct way to run code when a submit finishes, for example to:

  • show a toast ("Note added"),
  • clear the form (see #523),
  • move focus back to the first field,
  • navigate or close a dialog.

Today this has to be done by watching state in an effect:

const form = useForm('/notes?create', { title: '', body: '' });
const wasPending = useRef(false);

useEffect(() => {
  const settled = wasPending.current && !form.pending;
  wasPending.current = form.pending;
  if (settled && form.wasSuccessful) {
    toast.success('Note added');
  }
}, [form.pending, form.wasSuccessful]);

This is easy to get wrong. Watching wasSuccessful alone isn't enough, because it stays true until the next submission, so the effect has to track pending to detect each submit finishing.

Switching to action() works because it can be awaited, but then you lose useForm's typed data, field errors and pending.

Proposal

Callbacks in the useForm options, similar to Inertia's useForm:

const form = useForm(
  '/notes?create',
  { title: '', body: '' },
  {
    onSuccess: () => {
      form.reset();
      toast.success('Note added');
    },
    onError: (errors) => {
      /* validation errors or VoidActionError */
    },
  },
);

They could be called from inside the action in use-form.ts, right where wasSuccessful / errors / error are set today.

Ngôn ngữ chính
TypeScript
Star
402
Fork
32
Merge trung bình
10 giờ 51 phút
Pull request đã merge (30 ngày)
100

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

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.

Issue khác của voidzero-dev/void

Tất cả issue của voidzero-dev/void

Issue tương tự

Thêm issue về TypeScript

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.