Here is the flow:
componentDidMount() {
this.props.loadClient(); // dispatch an action from your component
}
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" component="input" type="text" />
</div>
<div>
<label htmlFor="email">Email</label>
<Field name="email" component="input" type="email" />
</div>
<div>
<label htmlFor="phoneno">PhoneNo</label>
<Field name="phoneNo" component="input" type="text" />
</div>
<button type="submit">Submit</button>
</form>
);
}
export default AddUser = reduxForm({ form: 'addUser', initialValues: {
name: "abc",
email: "abc@gmail.com",
phoneNo: "1234567890"
} })(AddUser)
Your component must have the Form component. 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