Search code examples
javascriptreactjsteams-toolkit

I want to check a checkbox if an element if an element belong to a list


I am developing a reactJS app. I am returning checkboxes and I want to check weather the checkbox I am returning has been already checked. When a checkbox is checked the value of that checkbox is added to a list which is stored on a db. When I display a checkbox I am trying to check if that value is already in the checked list. However, it doesn't work, nothing is checked. Is the If statement correct?

 const showSoftwares = () => {
      return contractor.softwares.map((item, index) => (
        <li key={index}>
          <input type="checkbox" {...contractor.done.includes(item) ? checked: true} onChange={handleToggle(item)}></input>
          <label>{item}</label>
        </li>
      ))
    };

screenshot

Thanks y'all for your help !!!


Solution

  • You could do like this:

    <input type="checkbox" defaultChecked={contractor.done.includes(item)} onChange={handleToggle(item)} />
    

    You can get more on following link: How to set default Checked in checkbox ReactJS?