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

71
Visualizações
Working with multiple checkboxes in react with one OnChange not woking

In my project, I have many checkboxes I'm working on and the goal is to use one onChange to keep track of checked box and store their values inside an array and remove when uncheck. But what i have done is returning an error. Can someone please tell me with what I'm doing wrong. codesandboxlink

the error i'm geeting

TypeError
selected.indexOf is not a function
OnChange
/src/App.js:11:24
   8 | 
   9 | const OnChange = (id)=>{
  10 |   let selected = isselected;
> 11 |   let find = selected.indexOf(id)
     |                      ^
  12 | 
  13 |   if(find > -1){
  14 |       selected.splice(find, 1)

Here is my code App.js

import React from "react";
import "./styles.css";
import {useState} from 'react'

export default function App() {
  const [isselected, setisselected] = useState([])


  const OnChange = (id)=>{
    let selected = isselected;
    let find = selected.indexOf(id)

    if(find > -1){
        selected.splice(find, 1)
    }else {
        selected.push(id)
    }
    setisselected({selected})

    console.log(isselected)
}



  return (
    <div className="App">
                          <div className="first-search">
                          <input
                            type="checkbox"
                            className="input-1"
                            value="Last 7 days"
                            name="last seven"
                            id= "1"
                            onChange={ (e)=>   OnChange(e.target.value)}
                            selected={ (e)=>  isselected.includes(e.target.value)}
                          />
                          <label htmlFor="">
                            Last 7 days
                          </label>
                        </div>



                        <div className="first-search">
                          <input
                            type="checkbox"
                            className="input-1"
                            name="last24"
                            value="last24"
                            id= "2"
                            onChange={ (e)=>   OnChange(e.target.value)}
                            selected={ (e)=>  isselected.includes(e.target.value)}
                          />
                          <label htmlFor="">
                            Last 24 hours 
                          </label>
                        </div>

  </div>
  );
}


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

0

setisselected({selected}) if this tries to reset isselected its been declared and is const !

note: name="last seven" and name="last24" value="last24" do not look good as personal coding convention.

*It would be good if you posted the error from a debug message from the browser. e.g. firefox or chrome.

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