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

[v8.0.4] Initial Values not available in Field componentWillMount

Open lewisheadden opened this issue 7 years ago • 18 comments

Are you submitting a bug report or a feature request?

Bug Report

What is the current behavior?

In componentWillMount for Field/FieldArray etc. components, field values are not set to their initialValues. See that in the console.log from componentWillMount in the sandbox the field value is "".

What is the expected behavior?

In 7.4.2, initial values were available in Field components during the componentWillMount lifecycle. See that the console.log now reads jim as per the provided initial value.

Sandbox Link

Redux Form 8.0.4: https://codesandbox.io/s/0yvykqqxwl Redux Form 7.4.2: https://codesandbox.io/s/p423x6y30

What's your environment?

Redux Form 8.0.4

Other information

Please let me know if you need any more clarity and thanks for all the work you do!

lewisheadden avatar Dec 14 '18 17:12 lewisheadden

I believe this is also the cause of my issue:

I am testing the upgrade to redux-form 8.0.4 using jest and enzyme's mount to mount a connected redux form with initialValues.

I am getting PropTypes warnings that required values are the wrong type (required boolean fields being passed empty strings). Logging renders I see a first render with empty string values, followed by a second render with the correct values. The first render seems to be generating the warning.

nerfologist avatar Dec 17 '18 09:12 nerfologist

Also get this error. props.input.value is always an empty string on componentWillMount, componentDidMount and first render call. I cannot initialize my forms without propTypes errors https://codesandbox.io/s/my7x8m83ox

alxmiron avatar Dec 17 '18 20:12 alxmiron

the same error with sync validation and array... I don't get initial value of array to validate function in first render

tomasztomys avatar Dec 19 '18 11:12 tomasztomys

Hi @erikras, any thoughts about this? May be any temp workaround? For my use cases redux-form v8 is completely broken because of this issue.

DavyJohnes avatar Dec 19 '18 18:12 DavyJohnes

componentWillMount is deprecated

jjosef avatar Dec 21 '18 16:12 jjosef

@jjosef You are correct. However, to clarify, moving this call to a constructor (as per the deprecation notification) shows the same problem.

If there's something else you think I should be doing, please let me know!

lewisheadden avatar Dec 21 '18 16:12 lewisheadden

I'm running into this same issue - I'm seeing the initialize action dispatched to Redux, the store is showing the correct values for both the values and initial keys, but then the individual Field components are all getting empty strings as values (however, if I inspect their meta props, the meta.initial prop is showing the correct initial value that the field should be using).

Has anyone had any luck getting around this?

srisonti avatar Jan 16 '19 04:01 srisonti

Also hitting this same issue with empty strings for props.input.value but only affecting a custom component that receives an JSON object for initialValue. All other normal fields seem to be working. Not managed to find a workaround yet.

chrishearn avatar Jan 23 '19 23:01 chrishearn

initialValues don't seem to be loaded into the redux form state until after an initial render. Several of my components rely on there being an initial value so I made a helper decorator for a quick fix for those components so I don't have to rewrite all my components, just decorate them. The decorator simply doesn't render the form until it's initialized.

reduxFormRequireInitialized.js

import React from 'react';

/**
 * redux-form introduced a breaking bug in 8.0 where forms are no longer initialized before the initial render. Some of our forms rely on this behavior. To avoid issues caused by this bug, we can decorate those forms with this decorator to hold rendering until the redux-form is initialized.
 */
export default Component => (
  props => {
    if (!props.initialized && props.initialValues !== undefined) {
      return null;
    }

    return (
      <Component {...props} />
    );
  }
);

MartijnHols avatar Feb 25 '19 14:02 MartijnHols

The decorator simply doesn't render the form until it's initialized

I suppose this can lead to blinks or UI shifts

alxmiron avatar Feb 25 '19 16:02 alxmiron

I've just encountered this as well. I think the problem is that react-redux has slightly changed behaviour after switching to the new context API. The initialize action is fired and the store is updated before rendering the connected field, but react-redux serves up a snapshot from when the render began. While this is annoying in this case, it does seem like a more correct behaviour.

I think it can be very simply fixed by reading value from initialValues instead of formState before initialization, a one-line change. I'll put together a pull-request.

kgram avatar Mar 12 '19 08:03 kgram

Redux Form 8.0.4: https://codesandbox.io/s/0yvykqqxwl will work better with constructor like this :

  constructor(props) {
    super(props)
    console.log(this.props.input.value);
  }

then modal doesn't render an empty string

GuillotJessica avatar Mar 13 '19 18:03 GuillotJessica

Issue is gone after updating to [email protected]

lynxtaa avatar Mar 26 '19 06:03 lynxtaa

Upgrading to [email protected] and [email protected] appears to have introduced this problem into my application. Upgrading to [email protected] and [email protected] does not appear to have got rid of it :(

pwhipp avatar May 29 '19 02:05 pwhipp

Any news on that facing the same issues...

eugensunic avatar Sep 17 '20 20:09 eugensunic

@eugensunic Please read the deprecation notice and consider using final form...

levino avatar Sep 18 '20 05:09 levino

@Levino I already have , currently on 7.4.2 and the next step in migration only.

eugensunic avatar Sep 22 '20 13:09 eugensunic

This might be resolved with 8.3.10. From a first look, I didn't encounter the problem anymore.

darekkay avatar Nov 10 '23 14:11 darekkay