Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!