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

174
Views
Pregunta sobre cómo agregar un valor de entrada a una matriz usando setState

Estoy tratando de usar setState para actualizar una matriz de estado en React usando una entrada HTML básica. El estado que estoy tratando de actualizar se ve así:

 "businessName": "", "grossAnnualSales": 0, "contactEmail": "", "numEmployees": 0, "annualPayroll": 0, "locations": [{"zip": ""}], "industryId": "",

Necesito agregar un código postal ingresado por el usuario en un componente React a este objeto en la matriz.

Hasta ahora he intentado esto y no funciona, solo se actualiza a una cadena y no a una matriz:

 updateZip(){ return e => this.setState({"locations": [{"zip": e.currentTarget.value}]})

}

El componente Reaccionar:

 <input onChange={this.updateZip()} type="text" className="zip-input"/>

¿Cómo puedo actualizar el estado con éxito?

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

0

use e.target.value y pase onChange={this.updateZip}

 class App extends Component { state = { locations: [{ zip: "" }] }; updateZip = (e) => { this.setState({ locations: [{ zip: e.target.value }] }); }; render() { return ( <div> <input onChange={this.updateZip} type="text" className="zip-input" /> <p>{this.state.locations[0].zip}</p> </div> ); } } export default App;

CódigoSandBox

about 4 years ago · Juan Pablo Isaza Report

0

Intente reemplazar su función updateZip con una función de flecha.

 updateZip = (e) => { return e => this.setState({"locations": [{"zip": e.currentTarget.value}]}) }

También

 <input onChange={(e) => this.updateZip(e)} type="text" className="zip-input"/>
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!