IDE telling me to not use $(document).ready in React

Viewed 879

I'm using query.dataTable in my React application and my IDE, Visual Studio Code, is showing me that I shouldn't use $(document).ready(function ()

enter image description here

My code:

...
import "datatables.net-dt/js/dataTables.dataTables"
import "datatables.net-dt/css/jquery.dataTables.min.css"

...

componentDidMount = async () => {
        this.props.onLoadData();
        this.setState({ events: this.props.data });

        $(document).ready(function () {
            $('#myTable').DataTable();
        });
}

...

return (
  ...

  <table id="myTable" className="table">

...

Why is the IDE showing it? The code works, I'm wondering why it is invalid.

Thanks

1 Answers

It could be telling you that the method is deprecated (or not the preferred modern way of doing things). Hover over the method call, and it shows you:

@deprecated — Deprecated since 3.0. Use jQuery(function() { }).

Try using this instead: just pass a function to jQuery:

$(() => {
  $('#myTable').DataTable();
});

By using JSDoc, you can produce the strikethrough yourself with @deprecated:

enter image description here

Related