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

219
Visualizações
How to change backgroundColor for Touchable Opacity using .map() in function?

So i am making a quiz app and the problem is i need to highlight checked answer button. I figured out how to do it with Refs, but it works for only three questions. How do i change it to work for the whole test. mapping through question answers

                <TouchableOpacity
                  key={answers.buttonId}
                  onPress={() => {
                    checkIfCorrect(answers.isCorrect);

                    resetColors(answers.buttonId);
                    highlightBlack(answers.buttonId);
                  }}
                  style={{
                    borderWidth: 1,
                    width: "80%",
                    margin: "5%",
                  }}
                >
                  <View ref={(el) => (myRefs.current[answers.buttonId] = el)}>
                    <Text
                      style={{
                        color: "grey",
                        fontSize: 32,
                        textAlign: "center",
                      }}
                    >
                      {answers.answerText}
                    </Text>
                  </View>
                </TouchableOpacity>
              ))}```
Array element with question
```{
      questionNumber: 3,
      questionText: "Solve 1 - 5x = 0.",
      questionImage: "",
      answers: [
        { answerText: "5", isCorrect: true },
        { answerText: "-1/5", isCorrect: false },
        { answerText: "1/5", isCorrect: true },
        { answerText: "4", isCorrect: false },
      ],
    },```

This how i use refs
```  const highlightBlack = (itemId) => {
    myRefs.current[itemId].setNativeProps({
      style: { backgroundColor: "black" },
    });
  };

  const resetColors = (itemId) => {
    myRefs.current.forEach((ref) => {
      if (ref !== itemId) {
        ref.setNativeProps({ style: { backgroundColor: "white" } });
      }
    });
  };```

It crashes after the 3rd question
about 4 years ago · Juan Pablo Isaza
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