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

225
Views
Recibo un error que no puede leer las propiedades de undefined (leyendo 'push') en reaccionar
24 | lastname: lname 25 | }; 26 | > 27 | setobj(obj.data.push(dat)); | ^ 28 | console.log(obj); 29 | }; 30 |

No lo entendí por primera vez, pero cuando vuelvo a escribir el valor del formulario y lo envío, me muestra estos problemas. ahí abajo está mi código completo.

 function App() { var [obj, setobj] = useState({ data: [ { name: "chetan", lastname: "vashisht" } ] }); var [name, setname] = useState(""); var [lname, setlname] = useState(""); const handle = (e) => { e.preventDefault(); const dat = { name: name, lastname: lname }; setobj(obj.data.push(dat)); console.log(obj); }; return ( <div className="App"> <form onSubmit={handle}> <label>Enter your name:</label> <input id="ing" type="text" value={name} onChange={(e) => setname(e.target.value)} required autoFocus /> <br /> <label>Enter last name:</label> <input type="text" value={lname} onChange={(e) => setlname(e.target.value)} required /> <br /> <button>Sign in</button> </form> </div> ); }

aquí hice un estado que es el objeto usando useState() donde tengo que almacenar los datos en obj.data, así que usé el método .push() para almacenar los datos. funciona bien al principio, pero luego, si almaceno valores nuevamente, me muestra un error.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Siempre debe pasar un nuevo valor a setobj .

 setobj(obj.data.push(dat));

Aquí está pasando el valor de retorno de push , que es la longitud de obj.data , que será primitivo del number de tipo. así que la próxima vez que vuelva a hacer clic en el push de inicio de signin , intentará acceder a un valor undefined .

SOULTION

CODESANDBOX DEMO

En su lugar, debe establecer el estado como:

 setobj({ ...obj, data: [...obj.data, dat] });
about 4 years ago · Santiago Trujillo 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!