question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able to set initial value with Redux Form

See original GitHub issue

So, I’m trying to set an initial value for my Select tag using Redux Form field.

Redux Field tag,

<Field
    name="vehicle"
    label="Vehicle"
    options={options}
    component={this.renderSelect}
/>

Function

renderSelect(props) {
    return (
        <Select  
            {...props.input}
            onBlur={() => { props.input.onBlur(props.input.value); }}
            options={props.options}
            placeholder={props.label}
            resetValue={props.meta.initial}
            simpleValue
        />
    );
}

Redux form maps to state

function mapStateToProps(state, initialProps) {
    return {
        initialValues: {
             vehicle: truck,
        },
    };
}

This is what I’ve tried.

The following code block successfully sets the Select tag to truck but while submitting the form I’m not getting {vehicle: 'truck'} in form values.

renderSelect(props) {
    return (
        <Select
            {...props.input}
            value={props.input.value ? props.input.value : props.meta.initial }
            onBlur={() => { props.input.onBlur(props.input.value); }}
            options={props.options}
            placeholder={props.label}
            resetValue={props.meta.initial}
            simpleValue
        />
    );
}

By including this.props.change(props.input.name, props.meta.initial); in rederSelect method I’m able to get the form value while submitting form but it leads to,

Warning: setState(...): Cannot update during an existing state transition (such as within render). Render methods should be a pure function of props and state.

What is the clear way to make the truck as an initial value of React select as well as getting it while submitting a form??

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

15reactions
MaxInMooncommented, Nov 11, 2017

Try to add enableReinitialize: true and/or keepDirtyOnReinitialize: true in your form export declaration:

export default reduxForm({
  form: 'formName',
  keepDirtyOnReinitialize: true,
  enableReinitialize: true, // fix issue "Redux Form - initialValues not updating with state" (http://stackoverflow.com/questions/38881324/redux-form-initialvalues-not-updating-with-state)
})(MyForm);


and try this onChange/onBlur behaviour:

const SelectAsync = (props) => {
  const { input, meta } = this.props;
  return (
    <Select.Async
      name={input.name}
      value={input.value}
      onChange={input.onChange}
      onBlur={() => input.onBlur(input.value)}
    >
  )
}

let me now!

4reactions
bitclawcommented, Aug 26, 2018

Also worth mentioning is that you should also add updateUnregisteredFields: true.

i.e:

export default reduxForm({
  form: 'formName',
  keepDirtyOnReinitialize: true,
  enableReinitialize: true,
  updateUnregisteredFields: true
})(MyForm);

From official docs:

updateUnregisteredFields : boolean [optional] Used in combination with keepDirtyOnReinitialize. Will update every initialValue which is still pristine. Normally only registered Fields will be updated. In most cases, this option should be set to true to work as expected and avoid edge cases. It defaults to false because of non-breaking backwards compatibility.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux-Form initial values from - reactjs - Stack Overflow
So is the problem that although your initialValues props get changed, your form does not get populated with the new data right? –...
Read more >
Initializing From State - Redux Form
Values provided to the initialValues prop or reduxForm() config parameter will be loaded into the form state and treated thereafter as "pristine". They...
Read more >
How to Use Initial Values in Redux Form - DevCamp
So this is a really simple fix all we need to do is go into this file newsletterEditForm.js and go to the redux...
Read more >
React JS Redux Form with Initial value, Validations and Props ...
To use Redux form you need to add redux-form reducer with our store as shown in below code. Create src/redux/ConfigureStore.js as follow. import ......
Read more >
React + Redux Form — Two ways to add initial values
We pass a prop called initialValues into the form instance component and the value is an object with the keys being the name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found