useForm()
Creating a form

Creating a form

Different from createForm, useForm do not use register function to register every input, but only the <form/> element. The only property required to register a inoput is the name property.

import { useForm } from "@createform/react";
 
function App() {
  const { register } = useForm();
  return (
    <form {...register()}>
      <input type="text" name="name" placeholder="Name" />
      <input type="submit" />
    </form>
  );
}

In the above example, we created a form with two inputs, name and submit. The submit input triggers the handleSubmit function.

Let's add some functions to handle submit, and reset the form.

import { useForm } from "@createform/react";
 
function App() {
  const { register } = useForm({
    onSubmit: handleSubmit
    onReset: handleReset
  });
 
  function handleSubmit(data) {
    console.log(data);
  }
 
    function handlereset() {
    console.log(data);
  }
 
  return (
    <form {...register()}>
      <input type="text" name="name" placeholder="Name" />
      <input type="submit" >Submit</input>
      <input type="reset" >Reset</input>
    </form>
  );
}