React-final-form: [BUG] FormStateToRedux - Maximum update depth exceeded

Created on 20 Sep 2018  路  19Comments  路  Source: final-form/react-final-form

All forms with initial values

No nested input names

Original simple example - codesandbox
Original example with connected Container - codesandbox

Nested input names

_Bug_
Original example with connected Container and nested initialValues & input names - codesandbox

_Fix_
Original example with connected Container & Form and nested initialValues & input names - codesandbox

bug

All 19 comments

I'm sorry, but I'm not seeing that error when typing in the fields. Can you give me some steps to reproduce?

Yeah, that's one of the ones above. I clicked on them all. What do I have to do to recreate the error?

The code is updated

fuck, something wrong in my example

updated again

const ConnectedReduxContainer = connect((state) => ({
  state: getFormState(state, "example")
}))(Container);

Ooh!!! This was an interesting one!!

The Form component is only doing a _shallow_ equals check on the initialValues prop to determine if it has changed. If it _has_ changed, then it reinitializes the form, which, in turn, was dispatching the update action via the FormSpy. That's why it works if the initialValues are flat, but not if they are deep.

I think that it probably needs to do a _deep_ equals check on initialValues, but for now you can work around it by not creating a new object of your initialValues every time the connected component rerenders. See lines 20-25:

Edit 馃弫 React Final Form - Redux Example

Personally, I would _not_ connect the container of the form to Redux in this way; I would only connect whatever particular deeper component needs that state. Makes sense?

Oh, my initialValues depended from BackEnd, it`s cant be static

That's fine, as long as it's the same object coming from Redux or whatever is loading them. You just can't _construct_ a new object when passing the prop.

That said, I will implement deep equals on the initialValues check.

as example i can define initial values in component constructor, makes sense?

Yep. As long as the object is only instantiated when they actually change, you should be fine.

when you planned fix it?

Yes, sorry. This week has been busy.

Okay, so deepEqual() is complicated. I wonder if we could allow an isEqual function to be passed in for initialValues checking....

@erikras would you consider using react-fast-compare to compare initialValues instead of allowing an isEqual function? Seems like a good alternative to solve the problem and it's pretty small too.

Published fix in v3.6.6.

@arthurdenner My reasoning on going with a function prop is that most people won't need it, as most forms are not deeply structured, but to allow flexibility for those that are.

@erikras Got it. Thanks for explaining it.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jkantr picture jkantr  路  4Comments

Soundvessel picture Soundvessel  路  4Comments

mewben picture mewben  路  3Comments

tpbowden picture tpbowden  路  4Comments

kavink picture kavink  路  5Comments