Last active
November 7, 2017 12:53
-
-
Save iamdanthedev/1ebe06090d8dbe0f56d445317427ca22 to your computer and use it in GitHub Desktop.
Typesafe redux-form field with custom props
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
/** | |
* Redux-form Field with custom property `nutrient` passed in directly and not via props | |
* Type safe and no typescript erros | |
*/ | |
import { Field, WrappedFieldProps } from 'redux-form'; | |
type ExtrasProps = { nutrient: Nutrient }; | |
const ValueField = Field as new () => Field<ExtraProps>; | |
// js output: const ValueField = Field; | |
return ( | |
<ValueField | |
name='member-name' | |
component={renderValueField} | |
nutrient={nutrient} // extra prop! | |
/> | |
); | |
/** | |
* Field renderer function (https://redux-form.com/7.1.2/docs/api/field.md/#2-a-stateless-function) | |
*/ | |
function renderValueField(field: WrappedFieldProps & ExtraProp) { | |
const { input, meta, nutrient } = field; | |
return ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment