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

331
Views
Handling multiple checkboxes in react

I want to click on multiple checkboxes in my react component and to handle which checkboxes are checked. i want to confirm when click on 'Verify' that name and address are checked by passing the checked state of each checkbox. can someone help me in that?

export class Form extends React.Component<FormProps, {
  ...,
  isChecked?: boolean
}> {

  constructor(props: FormProps) {
    super(props);

    this.state = {
      ...,
      isChecked:false
    };
    private checkField = () => {
    console.log('state is '+this.state.isChecked) 
     }
  }
  }
render() {
  return<Fragment>   
 <Input name="Name" required={true} handleChecked={(isChecked: boolean) => this.setState({isChecked})} />
 <Input name="Address" required={true} handleChecked={(isChecked: boolean) => this.setState({isChecked})} />
 <Input name="Birthdate" />
<div onClick={this.checkField}> Verify</div>
</Fragment>
  }
}

export function Input(props: React.PropsWithChildren<{name: string, required?: boolean, handleChecked?: (checked: boolean) => void}>) {
 
  const [isChecked, setIsChecked] = useState(false)
  
  return <Fragment>
    <div>
      {props.name}
      <label htmlFor={props.name}>Selection {props.required ? '(Required)': ''}</label>
      <input 
        id={props.name} 
        type='checkbox'
        name={props.name}
        checked={isChecked}
        onChange={
          setIsChecked(!isChecked)
          if(props.handleChecked) {
            props.handleChecked(!isChecked);
            }
          }
        }/>
    </div>
  </Fragment>;
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

As mentioned in the comment section, you need to create separate state for name and address checkbox value and pass it to the <Input> component.

class Wrapper extends Component {
  state = {
   isNameChecked: false,
   isAddressChecked: false,
  }

  render() {
   <Input
     name="Name"
     required={true}
     handleChecked={(isNameChecked: boolean) => this.setState({ isNameChecked })}
     isChecked={this.state.isNameChecked}
   />

   <Input
     name="Address"
     required={true}
     handleChecked={(isAddressChecked: boolean) => this.setState({ isAddressChecked })}
     isChecked={this.state.isAddressChecked}
   />

   <Input name="Birthdate" />
  }
}
export function Input(props: React.PropsWithChildren<{
 name: string,
 required?: boolean,
 handleChecked?: (checked: boolean) => void,
 isChecked: boolean,
}>) {
  return (
   <div>
      {props.name}
      <label htmlFor={props.name}>Selection {props.required ? '(Required)': ''}</label>
      <input 
        id={props.name} 
        type='checkbox'
        name={props.name}
        checked={isChecked}
        onChange={
          if (props.handleChecked) {
              props.handleChecked(!isChecked);
          }
        }
     }/>
    </div>
);
}
about 4 years ago · Juan Pablo Isaza Report

0

Can go about this in a few different ways..
I would suggest something like this:

interface State = {
  name: boolean;
  address: boolean;
}

class Wrapper extends Component {
  state: Readonly<State> = {
    name: false,
    address: false,
  }

  handleChecked = e => {
     setState({
       [e.target.name]: e.target.checked
     })
  }

  render() {
    <Input
      name="name"
      required
      handleChecked={handleChecked}
    />
    <Input
      name="address"
      required
      handleChecked={handleChecked}
    />
  }
}

function Child({ 
  name,
  handleChecked
}: { 
  name: string;
  handleChecked: (e: ChangeEvent<HTMLInputElement>) => void;
}) {
  return (
     <input 
        ...
        type='checkbox'
        name={name}
        onChange={handleChecked}
     />
  )
}

Then you could handle things based on the parent's state.
i.e. -

class Wrapper extends Component {
  ...

  foo = () => {
    if (state.name && state.email) {
       // do whatever..
    }
  }

  ...
}
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!