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

105
Views
How do i set sate based on the condition in checkbox Change function reactjs

In the checkbox the default value is null and I am setting it as true when required but also I thought of one more condition that needs to be set when the checkbox is already selected I want to unselect it and set the value as false instead of null.

Assume alwaysSelected -if this is selected it is true(onload) based on this condition i should update the state

this.state = {
checkbox: null,
}
this.props.alwaysSelected=true

<Checkbox
onChange={this.ChangeValue}
/>

ChangeValue= (event) => {
this.setState(checboxState=> ({
checkbox: checboxState.checkbox ? null : true
});
})

Just say prevState.checkbox -true on a load of the page then it just gives null but need to set it as false

Option tried is setState callback and componentDidUpdate but failed to acheive.

can we do this in another approach??

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can pass the checkbox state to the Checkbox component as value and change the event:

ChangeValue = () => this.setState({checkbox:!checkbox});

or if event returns true/false

 ChangeValue = event => this.setState({checkbox:!event});
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!