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

157
Views
How to change a "sub value" from an initial value?

I'm using Next.js with typescript.

I have a form with multiple inputs. I didn't want to make a useState for each input for obvious reasons and I didn't want to use a form-ready library either. I just wanted to find the solution to this.

How can I get the handleInputChange function to change the value of the director's name, for example, in the "initial state"?

const formInitialValues = {
  teamName: "",
  bornAt: "",
  logo: "",
  director: {
    name: "",
    email: "",
    phone: "",
  },
};

const [formData, setFormData] = useState(formInitialValues);

const handleInputChange = (e) => {
  const { name, value } = e.target;

  setFormData({
    ...formData,
    [name]: value,
  });
};

...
<input
  type="text"
  placeholder="Director name"
  name="directorName"
  id="directorName"
  required
  value={formData.director.name}
  onChange={handleInputChange}
/>
...
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do it like below:

const handleInputChange = (e) => {
  const { name, value } = e.target;

  setFormData(() => {
    ...formData,
    director: {
       ...formData.director,
       [name]: value  
    }
  });
};

or using callback of setter method:

setFormData((prevFormData) => ({
  ...prevFormData,
  director: {
    ...prevFormData.director,
    [name]: value
  }
}));
about 4 years ago · Juan Pablo Isaza 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!