Skip to content

Instantly share code, notes, and snippets.

@erikras
Created November 2, 2016 14:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save erikras/50b7b4bb4b4907b40a5ca9d99c2ff77e to your computer and use it in GitHub Desktop.
Save erikras/50b7b4bb4b4907b40a5ca9d99c2ff77e to your computer and use it in GitHub Desktop.
Example of initializing array fields from state
/**
* Answer to https://stackoverflow.com/questions/40349005/how-to-initialize-a-fieldarray-from-state-using-redux-form
*
* How to run:
*
* 1) Clone redux-form repo
* 2) Run the Initialize From State example (https://github.com/erikras/redux-form/tree/master/examples/initializeFromState)
* 3) Replace InitializeFromStateForm.js with this file
* 4) ...
* 5) Profit!
*/
import React from 'react'
import { connect } from 'react-redux'
import { Field, FieldArray, reduxForm } from 'redux-form'
import { load as loadAccount } from './account'
const data = { // used to populate "account" reducer when "Load" is clicked
firstName: 'Jane',
lastName: 'Doe',
age: '42',
sex: 'female',
employed: true,
favoriteColor: 'Blue',
bio: 'Born to write amazing Redux code.',
dogs: [ 'Rex', 'Fido', 'Snoopy', 'Brian' ]
}
const colors = [ 'Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet' ]
const renderDogs = ({ fields }) => (
<div>
{fields.map(field => <Field name={field} component="input"/>)}
</div>
)
let InitializeFromStateForm = props => {
const { handleSubmit, load, pristine, reset, submitting } = props
return (
<form onSubmit={handleSubmit}>
<div>
<button type="button" onClick={() => load(data)}>Load Account</button>
</div>
<div>
<label>First Name</label>
<div>
<Field name="firstName" component="input" type="text" placeholder="First Name"/>
</div>
</div>
<div>
<label>Last Name</label>
<div>
<Field name="lastName" component="input" type="text" placeholder="Last Name"/>
</div>
</div>
<div>
<label>Age</label>
<div>
<Field name="age" component="input" type="number" placeholder="Age"/>
</div>
</div>
<div>
<label>Dogs</label>
<FieldArray name="dogs" component={renderDogs} placeholder="Age"/>
</div>
<div>
<label>Sex</label>
<div>
<label><Field name="sex" component="input" type="radio" value="male"/> Male</label>
<label><Field name="sex" component="input" type="radio" value="female"/> Female</label>
</div>
</div>
<div>
<label>Favorite Color</label>
<div>
<Field name="favoriteColor" component="select">
<option value="">Select a color...</option>
{colors.map(colorOption =>
<option value={colorOption} key={colorOption}>{colorOption}</option>)}
</Field>
</div>
</div>
<div>
<label htmlFor="employed">Employed</label>
<div>
<Field name="employed" id="employed" component="input" type="checkbox"/>
</div>
</div>
<div>
<label>Bio</label>
<div>
<Field name="bio" component="textarea"/>
</div>
</div>
<div>
<button type="submit" disabled={pristine || submitting}>Submit</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>Undo Changes</button>
</div>
</form>
)
}
// Decorate with reduxForm(). It will read the initialValues prop provided by connect()
InitializeFromStateForm = reduxForm({
form: 'initializeFromState' // a unique identifier for this form
})(InitializeFromStateForm)
// You have to connect() to any reducers that you wish to connect to yourself
InitializeFromStateForm = connect(
state => ({
initialValues: state.account.data // pull initial values from account reducer
}),
{ load: loadAccount } // bind account loading action creator
)(InitializeFromStateForm)
export default InitializeFromStateForm
@vijay-mca
Copy link

please provide the output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment