Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
TypeError no capturado: no se pueden leer las propiedades de undefined (leyendo 'indefinido')

Estoy tratando de cambiar el estado creado con el enlace useState cuando se hace clic. Pero no entiendo este error. TypeError no capturado: no se pueden leer las propiedades de undefined (leyendo 'undefined'). No entiendo por qué sucede esto después de setState (state.activeQuestion + 1)

 import React, {useState} from 'react' import classes from './Quiz.module.css' import ActiveQuiz from '../../components/ActiveQuiz/ActiveQuiz' export default function Quiz () { const [state, setState] = useState( { activeQuestion: 0, quiz: [ { question: 'Якого коліру небо', rightAnswerId: 2, id: 1, answers: [ {text: 'чорний', id: 1}, {text: 'синій', id: 2}, {text: 'червоний', id: 3}, {text: 'зелений', id: 4} ] }, { question: 'Якому році 2 світова', rightAnswerId: 3, id: 2, answers: [ {text: '1954', id: 1}, {text: '1948', id: 2}, {text: '1949', id: 3}, {text: '1918', id: 4} ] } ],} ) const onAnswerClickHandler = answerId => { console.log('Answer Id: ', answerId); setState(state.activeQuestion + 1) } return( <div className={classes.Quiz}> <div className={classes.QuizWraper}> <h1> Дайте відповідь на всі Питання </h1> <ActiveQuiz answers={state.quiz[state.activeQuestion].answers} question={state.quiz[state.activeQuestion].question} onAnswerClick={onAnswerClickHandler} quizLength={state.quiz.length} answerNumber={state.activeQuestion + 1} /> </div> </div> ) }

ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

en su código, con onAnswerClickHandler está cambiando la forma de su estado. Su estado es un objeto y tiene diferentes valores.

 const onAnswerClickHandler = answerId => { console.log('Answer Id: ', answerId); setState(state.activeQuestion + 1) }

aquí estás cambiando tu estado a un número:

 state = {object stuff here}// //after you call the function state = 0 + 1 // all the other stuff is gone

como comentó @cybercoder, debe usar el operador de propagación:

 setState(prev=>({...state, activeQuestion : prev.activeQuestion + 1})) –

con el operador de propagación, copia todos los datos que contiene su objeto y solo actualiza el valor necesario que desea actualizar

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda