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

171
Views
one input in onChange function stored in React state is overwrite by another input

I have a React state object called formData with user checkout shipping data and inside formData there is another object called billingAdress which contains user billing data.

state = {
user: null,
formData: {
  name: null,
  company: null,
  adressOne: null,
  adressTwo: null,
  town: null,
  postcode: null,
  country: null,
  phone: null,
  isBillingAdress: false,
  billingAdress: {
    name: null,
    company: null,
    adressOne: null,
    adressTwo: null,
    town: null,
    postcode: null,
    country: null,
    phone: null,
  }}

There is onChange function on input

<label>Full Name:</label>
        <input
          name="name"
          onChange={this.handleBillingChange}
          value={this.state.formData.billingAdress.name}
        />

Function looks like this

handleBillingChange = event => {
const formData = { ...this.state.formData, billingAdress: {[event.target.name]: event.target.value }}
const errors = { ...this.state.errors, [event.target.name]: '' }
this.setState({ formData, errors})

}

Im trying to store whole billingAdress object inside of formData object, when I start typing in the first billing adress input, it works well, but when i start typing in a second input, first input disappear from a state.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem lies in your handleBillingChange function.

Try this in order to keep the current state of formData.billingAdress when changing one property.

const handleBillingChange = event => {
  const formData = {
    ...this.state.formData,
    billingAdress: {
      ...this.state.formData.billingAdress,
      [event.target.name]: event.target.value
    }
  }

  const errors = { ...this.state.errors, [event.target.name]: '' }
  this.setState({ formData, errors})
}
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!