Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda