Created
November 2, 2016 14:45
-
-
Save erikras/50b7b4bb4b4907b40a5ca9d99c2ff77e to your computer and use it in GitHub Desktop.
Example of initializing array fields from state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please provide the output