IDE 告诉我不要在 React 中使用 $(document).ready [英] IDE telling me to not use $(document).ready in React

查看:54
本文介绍了IDE 告诉我不要在 React 中使用 $(document).ready的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 React 应用程序中使用 query.dataTable,而我的 IDE Visual Studio Code 向我表明我不应该使用 $(document).ready(function ()>

我的代码:

<代码>...导入datatables.net-dt/js/dataTables.dataTables";导入datatables.net-dt/css/jquery.dataTables.min.css";...componentDidMount = async() =>{this.props.onLoadData();this.setState({ 事件: this.props.data });$(document).ready(function () {$('#myTable').DataTable();});}...返回 (...<table id="myTable";className=表">...

为什么 IDE 会显示它?代码有效,我想知道为什么它无效.

谢谢

解决方案

它可能告诉您该方法已被弃用(或不是首选的现代处理方式).将鼠标悬停在方法调用上,它会显示:

<块引用>

@deprecated — 从 3.0 开始弃用.使用 jQuery(function() { }).

尝试改用这个:只需将函数传递给 jQuery:

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

通过使用 JSDoc,您可以使用 @deprecated 自己生成删除线:

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 ()

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

解决方案

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:

这篇关于IDE 告诉我不要在 React 中使用 $(document).ready的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆