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

168
Visualizações
Interface type check with Typescript for ReactElement

I have the following Form component set up. I want it to only accept two types of children:

  1. A custom Input component, called InputComponent
  2. A normal HTML button element

I would like to generate the following output: {myInput1: "abc", myInput2: "def"}

Form.tsx

interface FormProps {
  children: Array<React.ReactElement<HTMLInputElement | HTMLButtonElement>>;
}

function Form({ children }: FormProps) {
  const handleSubmit = (event: React.SyntheticEvent) => {
    event.preventDefault();

    Children.map(children, (child) => {
      // ...
      // generate desired output here
      // ...
    });
  };

  return (
    <>
      <form onSubmit={handleSubmit}>
        <InputComponent name="myInput1" value="abc"/>
        <InputComponent name="myInput2" value="def"/>
        <button type="submit">Submit</button>
      </form>
    </>
  );
}

I attempted to check if a given child matches the interface of an HTMLInputElement or HTMLButtonELement, and extracting its name and value only if it is an HTMLInputElement. I am struggling to be able to make this differentiation and would appreciate any help I can get.

about 4 years ago · Juan Pablo Isaza
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