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

194
Vistas
How to change state value when input is checked with useState hook

I am trying to change the value of isChecked in state when checkbox is checked.

Here's my state:

const [talles, setTalles] = useState([
      { name: 'S', isChecked: false },
      { name: 'M', isChecked: false },
      { name: 'L', isChecked: false },
      { name: 'XL', isChecked: false },
])

For each element in this state an <input type="checkbox" /> is created.

The thing is I want to dynamically change the isChecked property.

Normally I would do:

setProduct({
       ...product,
       [e.target.name]: e.target.checked
})

But that would only work if my state was like useState({ productCheckbox: '' })

I would appreciate your help. Thanks in advance!

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

0

You can map the previous state and change the value of the changing talle:

setTalles(previousState => previousState.map(talle => {
  if (talle.name === e.target.name) return ({
    ...talle,
    isChecked: e.target.checked,
  })
  return talle
}))

Or same but shorter:

setTalles(previousState => previousState.map(talle =>
  talle.name === e.target.name ?
    ({
      ...talle,
      isChecked: e.target.checked,
    })
    :
    talle
}))


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