Forms
Inputs must have unique name
import { useForm } from "react-hook-form";
export default function App() {
const { register, handleSubmit, watch, formState: { errors } } = useForm();
const onSubmit = data => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input defaultValue="test" {...register("example")} />
<input {...register("exampleRequired", { required: true })} />
{errors.exampleRequired && <span>This field is required</span>}
<input type="submit" />
</form>
);
}List of validation rules supported: required | min | max | minLength | maxLength | pattern | validate (custom function)
Arrays
TextInput
Controlled Components
Normally form elements such as <input> maintain their own state and update based on input. To combine with React state, let React state be the “single source of truth” by updating it on change
React normalizes different tags like select, textarea, and text to have value attribute
Custom Hooks
Specific Tags
Select
becomes
Recall selected attribute in HTML choose what is initallly selected, value on main select tag does that in React
select multiple with <select multiple={true} value={['B', 'C']}>
Un Controlled
<input type="file" />
look above tedious b/c integrating with nonReact or converting codebase
Last updated