Formik 将定制的 Props 传递给定制的 Field 组件

Formik pass customized Props to custom Field Component

大家晚上好, 我在使用自定义 Formik 字段时遇到了一个小问题..

我正在使用自己的自定义 Field 例如:

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

FileUpload 道具看起来像:

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

字段和表单已正确传递到组件中,但如何将宽度和高度传递到组件中?试了一下,没成功。

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

Formik API 不幸的是没有提到如何解决这个问题。有没有人遇到过类似的问题并且知道如何解决这个问题?

非常感谢您的帮助!

如果您向 Field 传递了一个未在 Field 中使用的道具,它将被传递给 component 道具中的组件。

所以你可以这样做

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

并获取组件 props 中的值

const FileUpload = (props) => {
    // props.width
    // props.height
    return ( /* ... */ )
}