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

179
Vistas
React: Detect Selected option on Custom switch button

I have created a textual switch button using HTML and CSS. I am using that in a React app and I want to detect the selected option whenever it changes. for e.g. if OR is selected, it should be stored in selectedOption state and if AND is selected, that should be stored.

I have made a code sandbox with the demo (https://codesandbox.io/s/stupefied-snyder-lo530)

Can anyone please help me with this?

Thanks

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

set the state on click, by adding this:

onClick={(e) =>{ 
     selectedOption==="OR" ? setSelectedOption("AND") : setSelectedOption("OR"),
    console.log(selectedOption);
    }}
about 4 years ago · Juan Pablo Isaza Denunciar

0

As pointed out by @pilchard in comment, e.target.checked can be used to detect true/false and then based on that selectedOption can be set.

onClick={e => {
    const currentVal = (e.target as HTMLInputElement).checked;
    setSelectedOption(currentVal ? 'AND' : 'OR');
}}
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