Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-hook-form material ui file upload not giving FileList

i have an issue with react hook form and material-ui file uploading when submiting the form i got string path of one file instead of FileList instance

        <Controller
          name='attachments'
          control={control}
          defaultValue=''
          render={({ field }) => <input {...field} type='file' multiple />}
        />

       

full code on codesanbox:

https://codesandbox.io/s/xenodochial-bhaskara-9vo13?file=/src/App.js

like image 550
B. Fateh Avatar asked Nov 26 '25 12:11

B. Fateh


1 Answers

For it to work, you will have to implement your own onChange property. You can use the field.onChange callback for this purpose and pass it the file list as an argument. Here's how it can be done:

<Controller
  name="attachments"
  control={control}
  defaultValue=""
  render={({ field }) => (
    <input
      type="file"
      onChange={e => {
        field.onChange(e.target.files);
      }}
      multiple
    />
  )}
/>

Here is the link to the forked source code

like image 165
Kapobajza Avatar answered Nov 29 '25 02:11

Kapobajza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!