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

373
Views
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 answers
Answer question

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 Report

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 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!