#Is there a hook that can tell if the "current" form is being submitted?

1 messages · Page 1 of 1 (latest)

livid dock
#

I'm trying to make a generic SubmitButton that disables when the form it's within is submitting. I know I can use useNavigation to get pending state but I would need to check formData / formAction to know if the current submission is the right one. Is there another way to go about this?

livid dock
#

Is there a hook that can tell if the "current" form is being submitted?

thorny nymph
#

Can be achived by using a context wrapper. A good example of how such context can be implemented with ex. react-hook-forms, are available in shadcn-ui. - an easier approach, is to keep your button a pure component, and simply provide a "isSubmitting" prop, that also disables the button, and includes the correct aria attributes. 🙂

Shadnc-ui form: https://github.com/shadcn-ui/ui/blob/main/apps/www/registry/default/ui/form.tsx

React context: https://react.dev/reference/react/useContext

On keeping components pure: https://react.dev/learn/keeping-components-pure