1

I am trying to use react hook form for user input to upload file

import "./styles.css";
import { useForm, Controller } from "react-hook-form";
import {
  Col,
  Row,
  Form,
  FormGroup,
  InputGroup,
  Input,
  Container,
  Button
} from "reactstrap";

export default function App() {
  const onSubmit = (data) => {
    console.log(data);
  };

  const { control, handleSubmit } = useForm();

  return (
    <Container>
      <Form onSubmit={handleSubmit(onSubmit)}>
        <Row className="m-3">
          <Col>
            <FormGroup row className="mr-md-1">
              <InputGroup className="mb-3">
                <Controller
                  name="itemlist2"
                  control={control}
                  render={({ field: { ref, ...field } }) => (
                    <Input
                      {...field}
                      type="file"
                      required
                      innerRef={ref}
                      onChange={(e) => {
                        field.onChange(e.target.files);
                      }}
                    />
                  )}
                />
              </InputGroup>
            </FormGroup>
          </Col>
        </Row>
        <Button color="primary" className="mr-1">
          {"Save Changes"}
        </Button>
      </Form>
    </Container>
  );
}

Check on https://codesandbox.io/s/affectionate-moon-dmn8q

I get

enter image description here

Santhosh
  • 9,965
  • 20
  • 103
  • 243

2 Answers2

0

I'm not very familiar with reactstrap, but i think you have to omit the value prop which is part of field. You can’t set the value of an input with type="file". Check this answer for more infos.

<Controller
  name="itemlist2"
  control={control}
  render={({ field: { value, ...field } }) => (
    <Input
      {...field}
      type="file"
      required
      innerRef={field.ref}
      onChange={(e) => {
        field.onChange(e.target.files);
      }}
    />
  )}
/>

Edit blue-architecture-ncvut

knoefel
  • 5,991
  • 3
  • 29
  • 43
0

The issue is due to the fact that you are using a controlled input and not passing value to render it out on onSubmit.

Use { field: { value, ...field } } as props to the render method and it will set the 'value' property on 'HTMLInputElement' otherwise it will only pass an empty string which is in conflict with a file type data. It should be like this <input type="file" value="c:/js.txt"/> but you are doing like this <input type="file"/>

Complete Code:

import "./styles.css";
import { useForm, Controller } from "react-hook-form";
import {
  Col,
  Row,
  Form,
  FormGroup,
  InputGroup,
  Input,
  Container,
  Button
} from "reactstrap";

export default function App() {
  const onSubmit = (data) => {
    console.log(data);
  };

  const { control, handleSubmit } = useForm();

  return (
    <Container>
      <Form onSubmit={handleSubmit(onSubmit)}>
        <Row className="m-3">
          <Col>
            <FormGroup row className="mr-md-1">
              <InputGroup className="mb-3">
                <Controller
                  name="itemlist2"
                  control={control}
                  render={({ field: { value, ...field } }) => (
                    <Input
                      {...field}
                      type="file"
                      required
                      innerRef={field.ref}
                      onChange={(e) => {
                        field.onChange(e.target.files);
                      }}
                    />
                  )}
                />
              </InputGroup>
            </FormGroup>
          </Col>
        </Row>
        <Button color="primary" className="mr-1">
          {"Save Changes"}
        </Button>
      </Form>
    </Container>
  );
}
Ritik Banger
  • 2,107
  • 5
  • 25