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

139
Views
El estado de reacción no está disponible después de usar setState

Estoy trabajando en una aplicación de trivia usando ReactjS y cada vez que intento actualizar mi estado llamado "juego", aparece el siguiente mensaje de error:

 Uncaught TypeError: game is undefined.

Mi aplicación web está estructurada de la siguiente manera: Aplicación -> Pregunta -> Respuestas.

en la aplicación:

 const [game, setGame] = React.useState([]); function holdAnswer(qKey) { console.log(qKey); setGame((oldGame) => { oldGame.map((element) => { return qKey === element.key ? {} : element; }); }); console.log(qKey); } React.useEffect(function () { console.log("Effect ran"); fetch("https://opentdb.com/api.php?amount=5") .then((res) => res.json()) .then((data) => setGame( data.results.map(function (element) { return { ...element, key: uniqid(), answers: arrayShuffle([ ...element.incorrect_answers.map( (x) => new AnswerObj(x, false, uniqid()) ), new AnswerObj(element.correct_answer, false, uniqid()), ]), }; }) ) ); console.log(game); }, []); var wholeQ = game.map((element) => { return ( <Question wQ={element} holdAnswer={() => holdAnswer(element.key)} /> ); });

en el componente de la pregunta:

 export default function Question(props) { const answers = arrayShuffle(props.wQ.answers).map((element) => { return <Answers wholeAnswer={element} holdAnswer={props.holdAnswer} />; }); return ( <div className="question"> <p>{decode(props.wQ.question)}</p> <div className="answer-buttons-wrapper">{answers}</div> </div> ); }

en el componente de respuestas:

 export default function Answers(props) { return ( <button className="answer-button" onClick={props.holdAnswer}> {decode(props.wholeAnswer.answer)} </button> ); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Creo que el problema radica en el siguiente bloque:

 function holdAnswer(qKey) { console.log(qKey); setGame((oldGame) => { oldGame.map((element) => { return qKey === element.key ? {} : element; }); }); console.log(qKey); }

Como setGame termina sin devolver nada y por lo tanto establece el estado con un valor undefined .

Para solucionar esto, podemos eliminar las llaves en setGame para convertirlo en un "retorno implícito" .
Alternativamente, podemos agregar una declaración de devolución antes de la función de mapeo.

 // Either this -> setGame((oldGame) => oldGame.map((element) => { return qKey === element.key ? {} : element; }); ); // Or this -> setGame((oldGame) => { return oldGame.map((element) => { return qKey === element.key ? {} : element; }); });
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!