Not able to set initial value with Redux Form
See original GitHub issueSo, 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:
- Created 6 years ago
- Reactions:1
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Try to add
enableReinitialize: true
and/orkeepDirtyOnReinitialize: true
in your form export declaration:and try this
onChange/onBlur
behaviour:let me now!
Also worth mentioning is that you should also add updateUnregisteredFields: true.
i.e:
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.