-
I'm trying to add an <Input type="file" to my react-hook-form, but it submit a C:\fakepath when I submit the form.
|
Beta Was this translation helpful? Give feedback.
Answered by
luccasr73
Jul 9, 2021
Replies: 2 comments 2 replies
-
https://codesandbox.io/s/long-sun-nsfbk?file=/src/App.js import * as React from "react";
import { useController, useForm } from "react-hook-form";
import Headers from "./Header";
import "./styles.css";
let renderCount = 0;
const FileInput = ({ control, name }) => {
const { field } = useController({ control, name });
const [value, setValue] = React.useState("");
return (
<input
type="file"
value={value}
onChange={(e) => {
setValue(e.target.value);
field.onChange(e.target.files);
}}
/>
);
};
export default function App() {
const { register, handleSubmit, control } = useForm();
const onSubmit = (data) => console.log(data);
renderCount++;
return (
<div>
<Headers
renderCount={renderCount}
description="Performant, flexible and extensible forms with easy-to-use validation."
/>
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register("firstName")} placeholder="First Name" />
<FileInput name="file" control={control} />
<input type="submit" />
</form>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
I made this with setValue and registered field https://codesandbox.io/s/sample-using-react-hook-form-with-input-file-3s8l7 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
bluebill1049
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made this with setValue and registered field https://codesandbox.io/s/sample-using-react-hook-form-with-input-file-3s8l7