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

84
Visualizações
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 Respostas
Responde à pergunta

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 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