RussellHarrower
RussellHarrower

Reputation: 6790

useForm is called in function which is neither a React function component... error

the following code is making the strangest error.

/src/components/competitions/TheVaultform.js Line 5: React Hook "useForm" is called in function "contactform" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks

Search for the keywords to learn more about each error.

I have only just installed react-hook-form and ran the demo code.

import React from 'react';
import { useForm } from 'react-hook-form';

export default function contactform() {
  const { register, handleSubmit, errors } = useForm();
  const onSubmit = data => console.log(data);
  console.log(errors);

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input type="text" placeholder="First name" name="First name" ref={register({required: true, maxLength: 80})} />
      <input type="text" placeholder="Last name" name="Last name" ref={register({required: true, maxLength: 100})} />
      <input type="text" placeholder="Email" name="Email" ref={register({required: true, pattern: /^\S+@\S+$/i})} />
      <input type="tel" placeholder="Mobile number" name="Mobile number" ref={register({required: true, minLength: 6, maxLength: 12})} />
      <select name="Title" ref={register({ required: true })}>
        <option value="Mr">Mr</option>
        <option value="Mrs">Mrs</option>
        <option value="Miss">Miss</option>
        <option value="Dr">Dr</option>
      </select>

      <input name="Developer" type="radio" value="Yes" ref={register({ required: true })}/>
      <input name="Developer" type="radio" value="No" ref={register({ required: true })}/>

      <input type="submit" />
    </form>
  );
}

Upvotes: 4

Views: 3120

Answers (1)

Jap Mul
Jap Mul

Reputation: 18739

It doesn't recognize the React component because it should start with a capital letter, so change your component to:

export default function ContactForm() { ...

Upvotes: 15

Related Questions