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

238
Views
Uso de State Hook - (variable de estado) no está definido no undef
function Header() { const [keys, setKeys] = useState([]); //added const first = (e) => { var result = new Map() axios.post('http://localhost:8000/' + query) .then(function(response){ var content=JSON.parse(JSON.stringify(response)).data for (var i=0;i<content.data.length;i++){ result.set(content.data[i].image_id, content.data[i].caption) } var key = Array.from(result.keys()); setKeys(key); //added var value = Array.from(result.values()); }).catch(err => { console.log("error"); alert(err); }); }; const second = () => {} return ( <div> <button onClick={(e)=> { first(); second(); }}/> <img src={require(`img/img${key[0]}.jpg`)}/> </div> ); } export default Header;

Hice una pregunta aquí Usando estado con matriz, mapa / Cómo pasar variable y de las respuestas que obtuve, edité el código así. Sin embargo, sigo recibiendo este error --> 'clave' no está definida no undef

Pensé que tal vez tenía algo que ver con https://reactjs.org/docs/hooks-rules.html (Solo Call Hooks en el nivel superior). Pero no puedo entenderlo.

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

0

El problema es el error tipográfico.

Escribiste keys en lugar de key .

su código debe ser como:

 const [keys, setKeys] = useState([]) <img src={require(`img/img${keys[0]}.jpg`)}/>
about 4 years ago · Juan Pablo Isaza Report

0

Su variable de estado es keys , no clave. Si accede a la clave, obtendrá la clave no definida.

 return ( <div> <button onClick={(e)=> { first(); second(); }}/> <img src={require(`img/img${keys[0]}.jpg`)}/> </div> );
about 4 years ago · Juan Pablo Isaza Report

0

El problema no tiene nada que ver con lo que sospechabas.

 const [keys, setKeys] = useState([]); //added <img src={require(`img/img${key[0]}.jpg`)}/>

Ha definido el estado como keys , pero en su etiqueta img está accediendo a key[0] , por lo tanto, dice que la key no está definida

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!