-
Notifications
You must be signed in to change notification settings - Fork 47.6k
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
Document a way to "update" the defaultValue #3357
Comments
They can all share the same onChange-listener so I don't see the issue, but yeah some people don't like to do that for some reason.
There are two approaches for updating uncontrolled inputs, neither is universally right:
The second kind of applies to controlled components as well, but it's rarely something that really matters. It may be more obvious if you think of it in-terms of traditional DOM interaction, either you update the value of an existing input. Or e.g. when switching tabs you destroy the DOM and render it again and thus the newly rendered inputs show the current value. |
You mean setting It is the second case that I believe people want to implement the most when they run into this issue. But I haven't seen this solution presented anywhere. Along with the risks such as focus loss. |
|
What I want to do is
Through this I specify the listener once and not once per input field. |
@TheLudd You should be able to do exactly what you mentioned above. When the form changes just use |
@joshbedo Yes I know, but it generates warnings which makes it a no go for me (and many others who have commented on this here on github). I always strive to reduce the number of warnings to 0
|
yes, i am also want to bind onChange on form, not each form input field. but react give many warnings. |
How about a method like |
Struggling with the same issue, and thanks to @TheLudd I now at least know how to even remotely solve this (I added a dynamic key to my form, which is not the prettiest solution either). How did u get around the issue with the lost focus? |
I think the official answer is to use controlled components. There are other techniques, like using keys or the imperative DOM API, but these are escape hatches rather than officially endorsed solutions (documenting them beyond what's in the github issues would give them too much legitimacy). The confusion about |
Controlled components is not the same as having a reactive defaultValue. The former never lets you change it. The latter is necessary when it is necessary to change the default value for some reason but allow the user to enter their own value. |
There seems to be a lot of misunderstanding in the react community about how
defaultValue
works. It is often expected that if thedefaultValue
of an element is changed then that should be reflected in the UI. This is not the case sincedefaultValue
is only set when the component is first rendered.When people complain about this (#1484, #1663, #2764) it is usually suggested that they use
value
instead ofdefaultValue
but this is often seen as unpractical in a form were there are many input fields since all fields now need anonChange
listener to be editable. (This is not actually true, you can put anonChange
listener on a wrapping element but this generates a lot of warnings #1118, #3070).I feel that none of the above solutions are practical, but an easy solution is to set a
key
attribute on a wrapping element. If the form contains elements withdefaultValue
fields that are changed along with the key, the change will be reflected in the UI.I think this is a good solution for a common UI case where you have a list of entities you want to be able to edit. If you click one of them a form shows up with the entity value populated and if you click on another element you want the form to be updated with that entity's data.
Perhaps it could be documented in order to avoid future confusion. Or is there an even better solution?
The text was updated successfully, but these errors were encountered: