Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda