Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
I am using React Hook Form with Typescript, string does not accept incoming data

When I send {...register(name)} to my input component with the hook form, it gets an error, but when I send it as {...register("fullname")} it does not get an error. I can't use something like ("fullname") because I'm sending it using the map function.

register.tsx

import { Form, Input, PrimaryButton } from "components";

import { LoginRegisterLayout } from "layouts";

import { formTypes } from "constants/layouts/loginRegisterLayout";
import { getFormFields } from "constants/pages/register";
import { FormFieldType } from "utils/formFieldType";

import { useForm, SubmitHandler } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";

type Inputs = {
  fullname: string;
  email: string;
  password: string;
  passwordConfirmation: string;
};

const Register = () => {
  const {
    register,
    handleSubmit,
    watch,
    formState: { errors },
  } = useForm<Inputs>();
  const onSubmit: SubmitHandler<Inputs> = data => console.log(data);

  return (
    <LoginRegisterLayout formType={formTypes.REGISTER} headTitle="Register">
      <form onSubmit={handleSubmit(onSubmit)}>
        <>
          {getFormFields.map(
            (
              {
                id,
                name,
                label,
                placeholder,
                type,
                passwordField,
              }: FormFieldType,
              index: number
            ) => (
              <Input
                {...register(name)}
                label={label}
                id={id}
                name={name}
                key={index}
                placeholder={placeholder}
                type={type}
                passwordField={passwordField}
              />
            )
          )}

          <PrimaryButton size="xl" disabled={false}>
            Confirmation
          </PrimaryButton>
        </>
      </form>
    </LoginRegisterLayout>
  );
};

export default Register;

I've attached the error I got as a picture below.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try to define type like this

   {getFormFields.map(
            (
              {
                id,
                name,
                label,
                placeholder,
                type,
                passwordField,
              }: FormFieldType,
              index: number
            ) =>{ 
       const nam : Inputs = name
       return(
              <Input
                {...register(nam)}
                label={label}
                id={id}
                name={name}
                key={index}
                placeholder={placeholder}
                type={type}
                passwordField={passwordField}
              />
            )
}
          )}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda