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

83
Vistas
Handle checkboxes with useState object

I have some checkboxes displayed using .map() function which is retrieving the data from a JSON file.

What i have achieved so far, is to store the clicked checkbox into the state (which is an object). But after I click another checkbox, it removes the previous one and store the latest..

I basically want to store all the checked ones, and if you remove them to remove it from the state.

Here's my handleCheck() function:

const handleCheckInput = (
      e: React.ChangeEvent<HTMLInputElement>,
      key: number,
      questionId: number,
   ) => {
      let newTickArray = [...tick];
      newTickArray[key] = !tick[key];
      setTick(newTickArray);
      setValues({ ...values, [questionId]: e.target.value });
   };

Any help will be appreciated

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

0

Update:

Changed [questionId]: e.target.checked to [questionId]: e.target.value.

Answer:

Here is how you do it (I used ellipsis ... for parts that don't need to be changed and also for brevity):

(Also remember to use e.target.checked - the right property for checkbox elements - instead of e.target.value).

const handleCheckInput = (
  e: React.ChangeEvent<HTMLInputElement>,
  key: number,
  questionId: number) => {
    ... 

    if (e.target.checked) {
        setValues({ ...values, [questionId]: e.target.value});
    } else {
        // Remove unchecked from state.
        let { [questionId]: deletedKey, ...restOfObj } = values;
        setValues(restOfObj);
    }
}; 
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