Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda