react-redux-form icon indicating copy to clipboard operation
react-redux-form copied to clipboard

getForm doesn't return proper model for formReducer('') handling all forms - formValue is incorrect

Open marcinm2h opened this issue 7 years ago • 2 comments

The Problem

When formReducer handles all forms states (empty string as a model [just like here: https://davidkpiano.github.io/react-redux-form/docs/api/createForms.html]), <Form /> component's internal call of getForm (https://github.com/davidkpiano/react-redux-form/blob/master/src/components/form-component.js#L406) returns incorrect state.

It creates some errors when reading from $form state. For example: $form.intents, are never handled (memory leak), computing form-level validity is wrong and some others.

Steps to Reproduce

Create formReducer(''), that handles all forms states, check formValue of any <Form />.

Reproducible Code Example

https://codepen.io/marcinm2h/pen/GxjPrE

You can see in console formValue of <Form /> as it is logged in onUpdate handler.

marcinm2h avatar Mar 17 '18 18:03 marcinm2h

Seems like you know a probable fix, want to open a PR?

davidkpiano avatar Mar 19 '18 18:03 davidkpiano

I've found out that the problem occurs when I'm not passing initial state to formReducer(''), because initially state[formsReducerKey] doesn't have any sub-forms. Those are created on form actions.

Then this invalid formKey is cached, and later when I have state[formsReducerKey][subFormModel] in my state getFormStateKey(state, modelString, s) would compute it's model correctly, but it gets the incorrect one from cache/

I'm not sure if I should avoid cacheing on empty string model (''), or should just avoid initializing this kind of formReducer without initial state 🤔.

Edit: I'll try cacheing formStateKey for dynamic forms, when computed formStateKey's form model seems to be correct i.e. at least contains a part of getForm's argument model.

marcinm2h avatar Mar 19 '18 22:03 marcinm2h