dimanche 17 mai 2020

How to add select all checkbox to list of checkboxes in react

This is my initial state

class RoomAmmenitiesForm extends Form {
  state = {
    data: {
      free_toiletries: false,
      blackout_drapes: false,
      safe_deposit_box: false,
      lcd_tv: false,
      free_wifi: false,
      pay_tv: false,
    }}

This is my checkbox handler function

toggleCheckbox = (e) => {
    const AmmenityBoxName = e.target.name;
    this.setState((state) => ({
      data: {
        ...state.data,
        [AmmenityBoxName]: !state.data[AmmenityBoxName],
      },
    }));
  };

How do i add a function to handle checking and unchecking all checkboxes? Kindly assist




Aucun commentaire:

Enregistrer un commentaire