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

254
Visualizações
React Native toggle add/remove element in an array based off toggling a button

I have a TouchableOpacity that can be both pressed and unpressed.

Here is my useState variable:

const [isPressMonday, setIsPressMonday] = useState(false);

and here is my toggling function:

    setIsPressMonday(!isPressMonday);
    setDays(days => [...days, 'Monday']) 

  };

The thing is, when a user un-clicks the button, it should remove Monday from the array. This would be simple if I could just check if(!isPressMonday) and remove it from the array, but the default is false so that is almost always true

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

0

You don't need to check with isPressMonday state instead check if 'Monday' present in days state. If yes, remove else, add.

//assuming 'days' is your array state name
setIsPressMonday(isPressMonday => !isPressMonday);
const index = days.indexOf('Monday');
if (index !== -1) {
  let tempDays = days
  tempDays.splice(index, 1);
  setDays(tempDays)
}
else {
  setDays(days => [...days, 'Monday'])
}
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