1

I have a react component that is a table with each row (entry) having a <td> that is a checkbox. I use state to hold all those entries that have been checked and attached a click handler to a button to delete all those checked entries. The problem is that React renders part of the component based on diffing, so when my component is re-rendered after the delete, some of the new entries has their boxes checked already. How do I get rid of them?

Below is the render function of my component:

  render: function () {
    var that = this;
    var contacts = this.state.contacts.map(function (contact, index) {
      return (
        <tr key={index}>
          <td>{contact.firstName}</td>
          <td>{contact.lastName}</td>
          <td>{contact.city}</td>
          <td>{contact.phone}</td>
          <td>{contact.email}</td>
          <td><input type="checkbox" onClick={that.addToDeleteList.bind(that, contact.id)}/></td>
        </tr>
      );
});

1 Answer 1

1

You are letting the browser manage the state of those checkboxes.

You can bind checked with something like:

checked={that.state.deleteList.indexOf(contact.id) > -1}

That will bind whether the checkbox is checked to whether or not the id is in the deletelist. As long as you empty the deletelist on delete the boxes should be unchecked.

Sign up to request clarification or add additional context in comments.

1 Comment

amazing. you are amazing @ted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.