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

167
Visualizações
React quiz select answers list with css

In my quiz app, one questions have two answers.Currently when clicking answers it doesn't change color and cann't be selected. When answer is clicked it should change color and can be selected from list like radio button. when both options are selected(colored) we should submit.

            {questions[currentQuestion].answerOptions.map((answerOption) => (
              <button
                className="button"
                onClick={() => handleAnswerOptionClick(answerOption.isCorrect)}
              >
                {answerOption.answerText}
              </button>
            ))}

https://codesandbox.io/s/bold-jones-ec5ft7?file=/src/App.js

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

0

There is a lot of refactoring you need to do here:

  1. Merge all the functions of handleAnswerOptionClick, handleAnswerOptionClick1, handleAnswerOptionClick2 etc.. to one function with key index and value.
  2. Merge all the answerOptions into one data structure.

Use rule of thumb- if you write the exact code more the 3 times, you should consider using it with iterator like "for"

But to answer your question about changing the css of the right answer :

 <button
                className={"button " + (answerOption.isCorrect ? 'correctClass' : '')}
                
                onClick={() => handleAnswerOptionClick(answerOption.isCorrect)}
              > 

Css section-

.correctClass{
 background-color: lightblue;
}

I added a condition in the class name which apply new class when the answer is correct.

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