Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
Object Destructuring with React hook

There is a simple code:

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

export default function App() {
  const { register, formState: { errors }, handleSubmit } = useForm();
  
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register("firstName", { required: true })} />
      {errors.firstName?.type === 'required' && "First name is required"}
      
      <input {...register("lastName", { required: true })} />
      {errors.lastName && "Last name is required"}
      
      <input type="submit" />
    </form>
  );
}

form the official React Hook Form page.

My question regards the code:

const { register, formState: { errors }, handleSubmit } = useForm();

from the example above. I understand that it's just the Object Destructuring on hook function assignment but how the formState: { errors } works there and what does it really does?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Nested destructuring:

function nested() {
    return {
        really: { nested: { object: 42 } },
    };
}

const { really: { nested: { object } } } = nested();

console.log(object); // 42

Note that you cannot use really or nested as identifiers, only object, because that's what's being extracted.

about 4 years ago · Juan Pablo Isaza Report

0

Suppose your userForm() function is defined as

function useForm() {
    return {
        formState: {
            errors: {}
        }
    }
}

Then you may do something like

let { formState: { errors}} = useForm();

This is equallent to

let useFromResponse = userForm();
let errors = useFormResponse.formState.errors;

You are doing a nested destructing here and yaa once done you can use errors but not formState.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!