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

374
Visualizações
Setting formdata Object by using typescript

I have my Object state here written in vanilla javascript. and i wanted to convert it into typescript.

This is the state for formData object.

 const [formData, setFormData] = useState({
     firstName: "",
     lastName: "",  
 });

This is the handleFormChange function.

const handleFormChange = (event) => {
const { name, value } = event.target;
setForm(prevFormData => (
  ...prevFormData,
  [name]:value;
));

};

on vanilla js it's working perfectly fine. What could be the type definition for

[name]:value;

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

Input OnChange Getting the value from an input onChange event is one of the first things people learn when working with HTML. In a Typescript environment, it can be tempting to use any as a type for the onChange event. Instead of any , you can useReact.ChangeEvent<HTMLInputElement>

const onChange = (event: React.ChangeEvent<HTMLInputElement>) =>
//That will give you access to all the values included in the input onChange event.

If you are using a text area and need to differentiate the event, you can use this instead:

const onChange = (event: React.ChangeEvent<HTMLTextAreaElement>) =>
about 4 years ago · Santiago Gelvez Relatório

0

You should also share your setForm function here so I can try to answer your question based on that. Actually it would be even better if you could share your code with a CodeSandbox link. Btw this is not Vanilla JS, you use React's useState. I would suggest you to check React TypeScript CheetSheet.

about 4 years ago · Santiago Gelvez 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