0
    <div className="App">
        <div>
            <form
                onSubmit={handleSubmit((data) => {
                    console.log(data);
                })}
                className="hey"
            >
                <h2>Log in</h2>
                <Input
                    style="input"
                    placeholder="Email"
                    type="email"
                    {...register("Email")}
                />
                <Input
                    style="input"
                    placeholder="Password"
                    type="password"
                    {...register("Password")}
                />
                <Button style="button neon-transparent" type="submit" name="Log In" />
            </form>
        </div>
    </div>

enter image description here

I am currently in the process of trying to create a form using the react hooks form library, when I click submit on this form the object of the form gets console logged but the two fields appear as undefined when values have been put into each field. I know this is most likely an issue custom components because when I created the form with traditional input fields it worked no problem.

I am very new to this library, if anyone has an answer for how to use this library with custom components it would be much appreciated.

Here is the Input component as well:

    type InputProps = {
    type: string,
    placeholder?: string,
    style: string
}

export const Input = (props: InputProps) => {
  return (
    <input className={props.style} type={props.type} placeholder={props.placeholder} />
  )
}
4
  • where are your Input component coming from? Commented Apr 14, 2022 at 19:42
  • They're in a seperate TSX file would you like to see? Commented Apr 14, 2022 at 19:44
  • Do you have the register function defined above? If possible could you show the whole component? Commented Apr 14, 2022 at 19:47
  • Yes i use this const { register, handleSubmit } = useForm<LoginFormModel>(); Commented Apr 14, 2022 at 19:48

1 Answer 1

1

change your input component to the following:

export const Input = (props: InputProps) => {
   const {style, type, placeholder, ...others} = props;
   return (
        <input className={style} type={type} placeholder={placeholder} {...others} />
   );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer, is there a way to do it that doesnt required destructing the prop?
sure, you can send className instead of style as prop from your main component. Then you simply use <input {...props} />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.