我正在尝试使用React Hook表单上传文件,并且使用了USECONTROLLER函数,并且我会遇到错误

发布于 2025-02-07 07:16:30 字数 463 浏览 0 评论 0原文

我正在创建一个多部分形式,其中使用React Hook形式的USECORTOR方法。我声明默认值

const methods = useForm({
  defaultValues: {
    email: "",
    course: "Enginering",
    program: "B.tech",
    mobielNumber: "",
    dob: "",
    gender: "",
    cityName: "",
    state: "",
    file:"",
  },
});  

,但是我得到的文件值是null可以告诉我,如果我必须声明文件输入,以便我可以获得值。

我在那里看到了文件

文件输入输入将需要在应用程序级别进行管理 能够取消文件选择和filelist对象。

这些意味着我必须宣布它的地方。

I am creating a multipart form in which uses usecontrol method of react hook form . i declarer default value

const methods = useForm({
  defaultValues: {
    email: "",
    course: "Enginering",
    program: "B.tech",
    mobielNumber: "",
    dob: "",
    gender: "",
    cityName: "",
    state: "",
    file:"",
  },
});  

but i am getting file value is null can any one tell me were i have to declare file input so i can get value .

I seen documentation there they told

File typed input will need to be managed at the app level due to the
ability to cancel file selection and FileList object.

what these means where i have to declare it .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不奢求什么 2025-02-14 07:16:30

一天过后,我弄清楚了我们在使用USECONTROLLER发布文件时需要明确setValue的某些方式

,并且以某种方式 field 对象具有 value 的内部,它将得到e.target.value均为默认值,它类似于... fakepath/..不正确,因为文件在e.target.files内部上载

const { setValue } = useForm({
  defaultValues: {
     file: "",
    //.....
  }
})
const { field: { name, ref } } = useController({name, control, rules, defaultValue}); 

不包括 value ,因为usecontroller,因为 value 内部字段将导致错误如果与文件一起使用时,

const handleChange = (e) => {
   setValue('file', e.target.files);
}

<input name={name} ref={ref} type="file" multiple onChange={handleChange}/>

当您提交时,您将看到filelist,如果不是 facepath 正如我所说

After a day I figure it out that some how we need to explicitly setValue when we post file with useController

And also somehow the field object has value inside of it, it will get e.target.value as default and it something like ...fakepath/.. which not true because files upload inside e.target.files

const { setValue } = useForm({
  defaultValues: {
     file: "",
    //.....
  }
})
const { field: { name, ref } } = useController({name, control, rules, defaultValue}); 

Don't include value inside as a result of useController because the value inside field gonna cause the error if it use together with files

const handleChange = (e) => {
   setValue('file', e.target.files);
}

<input name={name} ref={ref} type="file" multiple onChange={handleChange}/>

When you submit it, you gonna see the FileList, if not or something like fakepath as i said you need to manually setValue to e.target.files

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文