首页 > 解决方案 > React Hook 表单和输入类型文件

问题描述

我正在尝试将输入类型文件与 React Hook Form 一起使用。

import { Controller, useForm } from "react-hook-form";
import {
    Button,
    Form, FormText,
    Label,
    Input,
} from 'reactstrap';

const Test = () => {
  
  const { handleSubmit, control, setValue, formState: { errors }} = useForm();

  // I have other inputs and divs. Just showing the file here
  return (
     <Controller
        name="file"
        control={control}
        render={({ field }) => (
           <Input {...field} type="file" id="file" />
        )}
     />
  );
}

当我提交表单并检查 data.file 它只有:C:\fakepath\myFile.pdf

const submitForm = (data) => {
   console.log(data.file);
}

标签: reactjsfilereact-hook-form

解决方案


推荐阅读