admin管理员组文章数量:1417470
I'm pretty new to React & Redux-Form and at the moment I am in need of some help.
Basically I have listing page with lists and their edit button. When the edit is clicked, I am showing a modal pop up with the fields and doing a API call to fetch the respective list data.
Could you please let me know how can I pre-populate the fields in the modal pop up with the data received from API call?
A demonstration with a code sample will be much appreciated(like I said I am pretty new to React & Redux & Redux-form). :(
Thanks in advance!
I'm pretty new to React & Redux-Form and at the moment I am in need of some help.
Basically I have listing page with lists and their edit button. When the edit is clicked, I am showing a modal pop up with the fields and doing a API call to fetch the respective list data.
Could you please let me know how can I pre-populate the fields in the modal pop up with the data received from API call?
A demonstration with a code sample will be much appreciated(like I said I am pretty new to React & Redux & Redux-form). :(
Thanks in advance!
Share Improve this question asked Mar 1, 2018 at 10:28 NeerajNeeraj 4839 silver badges17 bronze badges 1- Possible duplicate of programatically-change-redux-form-field-value – Shubham Khatri Commented Mar 1, 2018 at 10:38
4 Answers
Reset to default 5Here is the flow:
ponentDidMount() {
this.props.loadClient(); // dispatch an action from your ponent
}
action dispatcher
loadClient() {
// API call here
}
Store the result in redux reducer
case LOAD_PROFILE_SUCCESS:
return {
...state,
data: action.result // save your data here
};
Then add the initialValues props to the @connect decorator
@connect(state => ({
initialValues: state.profile.data // load the saved data here
}),
{ loadClient }
)
Example: You could load the intialvalues at reduxForm itself.
let AddUser = props => {
const { handleSubmit, initialValues } = props;
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="name">Name</label>
<Field name="name" ponent="input" type="text" />
</div>
<div>
<label htmlFor="email">Email</label>
<Field name="email" ponent="input" type="email" />
</div>
<div>
<label htmlFor="phoneno">PhoneNo</label>
<Field name="phoneNo" ponent="input" type="text" />
</div>
<button type="submit">Submit</button>
</form>
);
}
export default AddUser = reduxForm({ form: 'addUser', initialValues: {
name: "abc",
email: "[email protected]",
phoneNo: "1234567890"
} })(AddUser)
Your ponent must have the Form ponent. Rest will be taken care by redux-form.
Note:
Structure of your initialValues must same of form data. Field name and the object property name should be same.
for more detail, you could refer redux-form official page Here
If youre using redux, you just have to map your state
in initialValues
;
Here's how I do it:
const mapStateToProps = (state) => {
const { state } = state;
return {
initialValues: state,
}
}
then set enableReinitialize
to true
FormName = reduxForm({
form: 'formname',
enableReinitialize : true
})(FormName);
then connect
your app to your redux store
export default connect(mapStateToProps)(FormName);
Thank you @Sachidhanandhan!
Here's what I did -
- Defined the
reduxForm
withenableReinitialize: true
- In the
mapStateToProps
ofconnect()
, I initialized the data that I received via API and stored in State withinitialValues
- Connected the
reduxForm
withconnect()
=> This was the order I missed earlier
It is working like a charm now.
Cheers!
enableReinitialize : boolean [optional] When set to true, the form will reinitialize every time the initialValues prop changes. Defaults to false. If the keepDirtyOnReinitialize option is also set, the form will retain the value of dirty fields when reinitializing.
Set this to true
本文标签:
版权声明:本文标题:javascript - Redux-form: How to load values(edit mode) from a API call to form when in modal pop up? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745267637a2650699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论