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

135
Visualizações
How to change change and track boolean state of each element in an array

Im trying to make it so that when a TouchableHighlight is pressed in react native it goes from one colour to another. I have state that is set to false and changes when the button is pressed. However this changes the colour for all elements in the map rather than for each item individually. Is there a way to update the state for each element independently? Here is my code:

function OnboardingVibes() {
  const [pressed, setPressed] = useState(false);
  return (
      <View style={{marginTop: 40}}>
        <Text style={{fontSize: 22, color: '#FFF', marginBottom: 16}}>Vibes</Text>
        <View style={styles.buttonContainer}>
          {vibes.map((vibe) => {
            return (
                <TouchableHighlight onPress={() => setPressed(true)} style={{backgroundColor: pressed ? '#DDD' : '#4D2C5B', margin: 4, borderRadius: 4}} key={`vibe-${vibe}`}>
                  <Text style={styles.vibeButton}>{vibe}</Text>
                </TouchableHighlight>
            )
          })}
        </View>
      </View>
  );
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

One way to do it is to move the state down so you could have individual states. When the state is true at the top-level, all child components will receive the same state.

function TouchableVibe({vibe}) {
  const [pressed, setPressed] = useState(false);

  return (
    <TouchableHighlight
      onPress={() => setPressed(true)}
      style={{
        backgroundColor: pressed ? "#DDD" : "#4D2C5B",
        margin: 4,
        borderRadius: 4,
      }}
    >
      <Text style={styles.vibeButton}>{vibe}</Text>
    </TouchableHighlight>
  );
}

function OnboardingVibes() {
  return (
    <View style={{ marginTop: 40 }}>
      <Text style={{ fontSize: 22, color: "#FFF", marginBottom: 16 }}>
        Vibes
      </Text>
      <View style={styles.buttonContainer}>
        {vibes.map((vibe) => (
          <TouchableVibe key={`vibe-${vibe}`} vibe={vibe} />
        ))}
      </View>
    </View>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

const [pressed, setPressed] = useState(vibes.map(e=>false));
{vibes.map((vibe, index) => {
           return (
               <TouchableHighlight onPress={() => {
let new_Pressed =vibes.map(e=>false)
    new_pressed[index]=true;
setPressed(new_pressed);
}} 
style={{backgroundColor: pressed[index] 
                ? '#DDD' 
                : '#4D2C5B',
                margin: 4, borderRadius: 4}} key={`vibe-${vibe}`}>
                 <Text style={styles.vibeButton}>{vibe}</Text>
               </TouchableHighlight>
           )
         })}
about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of passing in a boolean to the state, pass in the id/vibe instead, and then in your conditional logic within the map you can determine if that single item is the same as the one you've clicked.

function OnboardingVibes() {
  const [pressed, setPressed] = useState('');
  return (
      <View style={{marginTop: 40}}>
        <Text style={{fontSize: 22, color: '#FFF', marginBottom: 16}}>Vibes</Text>
        <View style={styles.buttonContainer}>
          {vibes.map((vibe) => {
            return (
                <TouchableHighlight onPress={() => setPressed(vibe)} style={{backgroundColor: pressed === vibe ? '#DDD' : '#4D2C5B', margin: 4, borderRadius: 4}} key={`vibe-${vibe}`}>
                  <Text style={styles.vibeButton}>{vibe}</Text>
                </TouchableHighlight>
            )
          })}
        </View>
      </View>
  );
}
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