Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formik pass customized Props to custom Field Component

Good evening everyone, I am having a small issue with custom Formik Fields..

I am using an own custom Field such as:

<Field name="pdfFile" component={FileUpload} />

FileUpload Props look like:

interface FileUploadProps {
    field: FieldInputProps<any>; //These props should probably be derived from Formik
    form: FormikProps<any>;
    width: string;
    height: string;
}

field and form are correctly passed into the component, but how can I pass width and height into the component? Tried it like, but without success.

<Field 
    name="pdfFile" 
    component={(props: any) => (
        <FileUpload
            field={props.field}
            form={props.form}
            width={pdfWidth}
            height={pdfHeight}
        />
    )} 
/>  

Formik API unfortunately doesn't mention how to solve this problem. Has anyone experienced similar issues and knows how to resolve this?

Thanks a lot for your help!

like image 572
LikeAKemper Avatar asked Sep 05 '20 20:09

LikeAKemper


1 Answers

If you pass a prop to Field that isn't used in Field, it will be passed to the component in the component prop.

So you can do it like

<Field 
    name="pdfFile" 
    width={pdfWidth}
    height={pdfHeight}
    component={FileUpload} 
/> 

and get the value in the component props

const FileUpload = (props) => {
    // props.width
    // props.height
    return ( /* ... */ )
}
like image 65
Vencovsky Avatar answered Oct 13 '22 22:10

Vencovsky