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

Pass initialValue to a <Field> #4737

Open
thany opened this issue Jun 1, 2021 · 3 comments
Open

Pass initialValue to a <Field> #4737

thany opened this issue Jun 1, 2021 · 3 comments
Labels

Comments

@thany
Copy link

thany commented Jun 1, 2021

Subject of the feature

Should be possible to pass an initial value to a <Field>.

Problem

You can't always pass initialValues into the reduxForm() call, because that's not where initial values are determined, especially when creating a reusable field, where the name of said field is yet unknown, but you do want to supply a initial value right then and there.

The initialize() action is also not ideal, because there you would have to know the form name, which might very well not be known at that time, and the full path of whereever a reusable field happens to be used, which is not neccesarily known right then and there.

Adding an initial value to a field should be super easy. I feel it's been designed to be as hard as possible when dealing with complex scenarios (which, let's be honest is exactly what you'd use redux-form for, you wouldn't use it for only a newsletter form). Examples for this are always the simple examples, but a complex scenario is not a simple form. The reduxForm() call is nowhere near the fields it contains.

Expected behavior

Something like <Field {...} initial="foo" /> when creating the field. Or perhaps using a decorator in some way. Something utterly simple, because an initial value is something that is (or rather, should be) super simple to reason about.

Say for example I want every country field to have an initial value. So I build this hypothetical country field. And then what? Where would I put its initial value? In the form around it, is the answer. In every form that happens to contain it.

Alternatives

Fetching the form name from somewhere, fetching the full path to the field somehow (I haven't figured it out yet) and passing that into an action creator that dispatches yet another action to get just one field to initialize. This feels like cracking a nut with a gas-powered sledgehammer.

@thany thany added the feature label Jun 1, 2021
@povilass
Copy link

  1. reduxForm returns initialize to InjectedFormComponent which actually know the name of the form.
  2. To initialize value on the field is not optimal because you gonna hit multiple events to update the store. Even not using redux-forms or any other form lib you want to initialize only once/twice on some cases with prefilled data, multiple updates is not the solution in your case.

The is a difference between controlled form and uncontrolled form. Uncontrolled forms do not have other solutions on how to set their values initially (for example material UI input with defaultValue property) and it does not impact the application/page when you work with the controlled form you always want to do this once because you don't have multiple renders.

https://codesandbox.io/s/redux-form-initialize-from-state-forked-zi92m?file=/InitializeFromStateForm.js

@thany
Copy link
Author

thany commented Nov 23, 2021

  1. The form is initialized "far away" from the component in it. Your simple example cannot apply in complex scenarios. Maybe most people do make simple forms, but we don't.
  2. We currently already do fire "initialize" calls in the form of CHANGE dispatches for each and every initialized field. So if a true field-by-field initialization would fire a dispatch for each field, we wouldn't loose any performance.

@povilass
Copy link

I have experience in "far away" from and my forms are even interpreters+wizards. Probably you just don't know how to solve you know example. When you got interpreter forms defaultValue on fields are overkill.

Look at any other forms tool none of them got initial value because YOU WILL HAVE PERFORMANCE issues because each dispatch to redux, component or any mutation if you gonna have ~100 fields will make your page laggy. You need a bigger picture of how it should work all the cases when you got global initialize and field initialize, what is the expected behaviour would that when you mix those. You field component rerenders does he have to update that state part or it should be the same as it has been before, how enableReinitialize would work on global and field initialization. None of those cases is described only one side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants