Skip to content

Instantly share code, notes, and snippets.

@vasanthk
Forked from erikras/example.js
Created February 7, 2017 22:23
Show Gist options
  • Save vasanthk/37b46cb60b8bdb04586cdf511211c752 to your computer and use it in GitHub Desktop.
Save vasanthk/37b46cb60b8bdb04586cdf511211c752 to your computer and use it in GitHub Desktop.
Redux Form v5 -> v6 Migration Hint
/*
* As I have been migrating a project from redux-form v5 to v6, I have come across this useful pattern.
*
* I have many such "custom inputs and errors beside them" structures around my app.
*/
// v5 code
<div>
<MyCustomInputThing
{...myField}
customProp1="My label"
customProp2="Options array"/>
{myField.touched && myField.error <span>{myField.error}</span>
</div>
// v6 code
const renderInput = ({ componentType, meta: { touched, error }, ...props }) => {
const ComponentType = componentType // gotta capitalize it for React to treat it properly
return (
<div>
<ComponentType {...props}/>
{touched && error && <em>{error}</em>}
</div>
)
}
...
<Field name="myField"
component={renderInput}
componentType={MyCustomInputThing}
customProp1="My label"
customProp2="Options array"/>
/*
* Note that you will need to either teach MyCustomInputThing that the value, onChange, onBlur, etc.
* is under the "input" key (which is worth doing so that it could be used directly with Field) or
* have renderInput() pull those out for you.
*/
// Happy migrating, @erikras
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment