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

176
Views
Reaccionar: ¿cómo puedo agregar un nuevo valor a una matriz y mostrar esta matriz en la ventana de alerta?

Estoy escribiendo un programa realmente simple pero tengo un pequeño problema. Cuando escribo un nuevo presagio en la etiqueta de entrada, quiero agregarlo a la matriz que está en estado y mostrar todo el contenido de la matriz en el cuadro de alerta. ¿Cómo puedo hacerlo de la manera más fácil?

 class Draw extends React.Component { state = { omen: "", omens: ["first", "second", "third"], newOmen: "", }; handleRandomOmen = () => { const omensArray = this.state.omens; const randomOmen = omensArray[Math.floor(Math.random() * omensArray.length)]; this.setState({ omen: randomOmen, }); }; handleChange = (e) => { this.setState({ newOmen: e.target.value, }); }; handleAddOmen = () => { const test = this.state.omens.push(this.state.newOmen); console.log(test); }; render() { return ( <div className="app"> <button onClick={this.handleRandomOmen}>Show omen</button> <br /> <input type="text" value={this.state.newOmen} onChange={this.handleChange} /> <button onClick={this.handleAddOmen}>Add omen</button> <h1>{this.state.omen}</h1> </div> ); } } ReactDOM.render(<Draw />, document.getElementById("root"));

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

0

siempre necesita actualizar un estado con setState , agregar un nuevo valor de matriz con push será anulado por el próximo ciclo de reacción.

Puede usar su sintaxis extendida para crear una nueva matriz con los valores antiguos + su nueva:

 handleAddOmen = () => { this.setState({ omens: [ ...this.state.omens, this.state.newOmen ] }); };
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!