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

360
Views
Intentando agregar a React State Array usando spread o push, Error: TypeError: la matriz no es iterable

Creé una matriz en el objeto de estado inicial y estoy tratando de agregar elementos a la matriz cada vez que se marca una casilla de verificación y eliminar el elemento cada vez que no está marcada.

La función que estoy usando es bastante simple y no veo ninguna razón por la que no debería funcionar. Estoy usando setState para distribuir los elementos en la matriz y agregar el nuevo elemento al final, pero sigo recibiendo un TypeError en el navegador que dice que la matriz no es iterable. Intenté usar typeof para ver si es una matriz y da como resultado undefined .

Haga clic aquí para ver el error que recibo

mi código es:

 class Step3 extends React.Component { constructor() { super() this.state = { missingList: [], } } handleChangeArray = e => { this.setState({ missingList: [...this.state.missingList, e.target.name] }) console.log(this.state.missingList) } render() { const { currentProductType, ProductSubtype } = this.props return ( <div className="row mt-4"> <h4 className='subtitle'>Missing List</h4> { systemData[`${currentProductType}`][`${ProductSubtype}`].parts.map((item, _id) => <CustomCheckbox col={3} key={_id} name={item} text={item} onChange={this.handleChangeArray} /> ) } </div> ) } }

systemData apunta a un objeto anidado de donde proviene la lista y usa el método de map para representar la casilla de verificación

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

0

Bueno, es porque handleChange no está vinculado a la clase Step3 . Entonces, cuando CustomCheckBox invoca handleChange , el contexto de this se referiría a CustomCheckBox y no a Step3 . Entonces, this.missingList está correctamente undefined . Es simplemente el alcance de this .

Verifique este reproductor mínimo de su caso de uso. Comentar la línea de bind dará como resultado el error que ha publicado. https://codesandbox.io/embed/upbeat-smoke-q0iei?fontsize=14&hidenavigation=1&theme=dark

Lectura adicional: https://reactjs.org/docs/handling-events.html

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!