Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormField usage examples mix controlled & uncontrolled input API #384

Closed
Moshyfawn opened this issue May 20, 2023 · 2 comments · Fixed by #385
Closed

FormField usage examples mix controlled & uncontrolled input API #384

Moshyfawn opened this issue May 20, 2023 · 2 comments · Fixed by #385

Comments

@Moshyfawn
Copy link
Contributor

Moshyfawn commented May 20, 2023

Context

https://twitter.com/moshyfawn/status/1659669594217357312

Description

The React Hook Form API is designed with two input handling techniques in mind: controlled inputs and uncontrolled inputs.

In controlled inputs, the input value received from the change event is stored in an external storage such as React state. This external storage holds the value, which is then passed back to the input, effectively controlling the input value. This approach is facilitated by the useController / Controller API.

On the other hand, uncontrolled inputs have the input itself as the source of truth for the value. In this case, the register method is responsible for registering the input event handler, but no value is passed down to the input. The input component maintains its own internal state for the value.

Mixing these two APIs can result in undesired behavior or difficult-to-debug input issues. Therefore, it is generally discouraged and unsupported by React Hook Form.

Side Note №1

There's also an example usage of this library with useFieldArray where the anti-pattern described above is being used. This one requires a bit more thought and potentially a separate FormFieldArray primitive component. I'm just tired at this time, so I left it untouched for the time being.

Side Note №2

It is generally recommended to use the register method for input components that can be easily utilized as uncontrolled, such as this library's Input component. However, implementing this approach would require a significant rethinking of the FormField API and dynamically determining whether the child input should be controlled or uncontrolled.

I will attempt to create a draft for this in the future. If you're also interested, please remind me if I forget to do so within the next couple of days. 😋

@shadcn
Copy link
Collaborator

shadcn commented May 20, 2023

There's also an example usage of this library with useFieldArray where the anti-pattern described above is being used. This one requires a bit more thought and potentially a separate FormFieldArray primitive component. I'm just tired at this time, so I left it untouched for the time being.

Yes. Ping me when you're available. Thanks so much for your help.

@Moshyfawn
Copy link
Contributor Author

Yes. Ping me when you're available. Thanks so much for your help.

For sure! I'm glad I could help 🙏

shadcn added a commit that referenced this issue May 20, 2023
* refactor: remove usage of uncontrolled input apis within controlled inputs

Closes: #384

* docs: display FormField controlled input usage example

---------

Co-authored-by: shadcn <[email protected]>
suleymanbariseser pushed a commit to suleymanbariseser/shadcn-ui that referenced this issue Jul 25, 2023
* refactor: remove usage of uncontrolled input apis within controlled inputs

Closes: shadcn-ui#384

* docs: display FormField controlled input usage example

---------

Co-authored-by: shadcn <[email protected]>
kjxbyz pushed a commit to muse-ui/muse-ui that referenced this issue Jun 7, 2024
* refactor: remove usage of uncontrolled input apis within controlled inputs

Closes: shadcn-ui#384

* docs: display FormField controlled input usage example

---------

Co-authored-by: shadcn <[email protected]>
u007 pushed a commit to u007/ui that referenced this issue Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants