Skip to content

Instantly share code, notes, and snippets.

@iamdanthedev
Last active November 7, 2017 12:53
Show Gist options
  • Save iamdanthedev/1ebe06090d8dbe0f56d445317427ca22 to your computer and use it in GitHub Desktop.
Save iamdanthedev/1ebe06090d8dbe0f56d445317427ca22 to your computer and use it in GitHub Desktop.
Typesafe redux-form field with custom props
/**
* 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