1

In my React application, I am using jquery Datatable table.

Initially the table loads fine. Whenever I am trying to override the data with a new data, it is throwing the error "Invariant Violation: processUpdates(): Unable to find child 10 of element. This probably means the DOM was unexpectedly mutated.."

I searched on this issue. What I understood is since the datatable plugin is directly mutating the DOM, React is getting confused on state change. One solution I got is call the datatable on willComponentUpdate. But, it is not working for me. Not sure on how to solve this issue.

Thanks for helping.

$('#topicsTable')
  .addClass('initialized')
  .dataTable({
    order: [
      [4, "desc"]
    ],
    columnDefs: [{
      targets: [-1],
      className: 'hidden'
    }]
  });

enter image description here

1 Answer 1

2

I have been engaged in the same battle for the last day and a half. At this point, I am considering ditching Datatables for a native React grid like Griddle.

That being said, you can force it to work by destroying and reinitializing Datatables before React does any work on the DOM. Use the component lifecycle to do this:

  // Initialize Datatable after React has rendered
  componentDidMount() {
    $('#dataTable').dataTable(dataTableStyle);
  },

  // Destroy Datatable before any component updates
  componentWillUpdate(nextProps, nextState) {
    $('#dataTable').dataTable().fnDestroy();
  },

  // Re-initialize Datatable after React has rendered
  componentDidUpdate(prevProps, prevState) {
    $('#dataTable').dataTable(dataTableStyle);
  },

To take it a step further, you can use check the next/previous props and state in order to determine if you need to actually do a render or not.

My problem with this solution is that its too slow and I get ugly reflows when operating on tables with lots of data. It might work for less complex grids however.

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

3 Comments

This is really helpful. I tried to destroy like you mentioned in componentWillUpdate. However, it started throwing errors like 'cannot find parentNode of undefined'. It will not throw error first time on fnDestroy. But second time when I give new data, it is giving the error.
It is throwing the error on the second call of dnDestory? Is the #dataTable element still on the DOM?
You are right. Throwing error on 2nd call and the #dataTable element is still on DOM

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.