- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 jQuery 和 ReactJS 来完成一些事情。我注意到,当我在 React 中执行此操作与直接在浏览器控制台中执行操作时, $(selector)
的输出是不同的。
由于这个原因,我直接在浏览器控制台中执行的某些 JavaScript 代码可以工作,但当我在 React 中编写它时它就不起作用。
例如我有这个表:
render(){
console.log(this.state.data);
return (
<Table striped bordered condensed hover id="files-table">
<thead>
<tr>
<th>stuff</th>
</tr>
</thead>
<tbody>
<tr>
<td>stuff</td>
</tr>
</tbody>
</Table>
);
}
一方面,当我在 React 中执行 console.log($('#files-table'))
时,比如说在 componentDidUpdate()
Hook 中,我在控制台中查看此输出:
...另一方面,直接在浏览器控制台中键入的相同控制台语句显示此输出(这是我期望的输出):
有人可以解释一下这些差异吗?
我在实现 jQuery DataTables 时遇到问题,因为将 $('#files-table').DataTable()
放入 React componentDidUpdate()
Hook 会给出一个错误: $(...).DataTable 不是函数 虽然它可以在浏览器中工作(我知道这个错误可能是由于加载 jQuery 两次等引起的,但我不认为这是案例在这里)。
最佳答案
这种差异是由于 ReactJS 使用虚拟 DOM 而导致的,当您在浏览器控制台中执行 jQuery 选择器时 - 您正在访问实际的(真正的)DOM。
ReactJS 不是直接接触真实的 DOM,而是构建它的抽象版本(副本)。 ReactJS 使用这种方法解决的主要问题是 DOM 从未针对创建动态 UI 进行优化,并且写入浏览器的 DOM 相对较慢。
将虚拟 DOM 视为实际 DOM 的轻量级副本。与实际 DOM 一样,虚拟 DOM 是一个节点树,它将元素及其属性和内容列为对象和属性。 React 的 render() 方法从 React 组件创建一个节点树,并更新该树以响应由操作引起的数据模型中的变化。
One would think that re-rendering the entire Virtual DOM every time there’s a possibility that something has changed would be wasteful — not to mention the fact that at any one time, React is keeping two Virtual DOM trees in memory. But, the truth is that rendering the Virtual DOM will always be faster than rendering a UI in the actual browser DOM. It doesn’t matter what browser you’re using: this is just a fact.
React does this magic by attaching attributes to elements in your document and manipulating them individually (using these very specific ID attributes) after doing the diff to determine what needs updating. The Virtual DOM inserts additional steps into the process, but it creates an elegant way to do minimal updates to the browser window without you having to worry about the actual methods being used or even what needs to be updated and when.
有关虚拟 DOM 和实际 DOM 之间差异的更多信息,您可以找到 explained in this article并在 ReactJS docs也是如此。
在你的例子中 - jQuery DataTabes 库修改了 DOM。所以,你需要让 React 远离它。当 React 完全控制 DOM 时,它的效果最好。但在你的情况下 - 你需要将控制传递给 jQuery。
您需要创建一个组件来管理 jQuery DataTabes。该组件将提供 jQuery 组件的以 React 为中心的 View 。此外,它将:
看看 my answer here 。它包含如何在 ReactJS 中集成 jQuery 库的完整细节和详细说明。您可以使用相同的方法。
关于javascript - 与浏览器控制台相比,为什么相同的 jQuery 选择器在 React Component hooks 中具有不同的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38603216/
这个问题在这里已经有了答案: 11年前关闭。 Possible Duplicate: Haskell: difference between . (dot) and $ (dollar sign) 好
我对 Java 平台没有任何了解,我想知道可以使用哪些工具(和方法)来帮助开发用 Java 编写的可维护代码。 我知道可以使用: 适用于任何环境的敏捷方法 用于单元测试代码的 jUnit/jMock(
我们的产品需要支持 IE9,但我们一直假设 IE9 支持 IE10+ CSS 规则。 是否有一种巧妙的方法来获取在 IE10+ 中有效但在 IE9 中不受支持的所有 CSS 规则,目的是在静态代码分析
我需要为 MyString 类重载运算符 + 和 +=。 MyString.h class MyString { char* m_pStr; }; 主要
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在升级现有的旧代码以使用 VS 2019*,在代码中我有以下函数在返回行失败: int foo(const char *fn) const { ofstream out(fn,ios::b
我想使用 R2D3 pacakge 在 R 中,但我不确定这个包与 D3.js 库的关系。 R2D3 是否以任何方式限制 D3 的功能?我们可以将 R 中的所有 D3 功能和特性与 R2D3 一起使用
我正在使用 WPF 语音识别库,试图在桌面应用程序中使用它来替代菜单命令。 (我想专注于没有键盘的平板电脑体验)。它可以工作 - 有点,除了识别的准确性太差以至于无法使用。所以我试着听写到 Word。
我在学校参加数据库类(class)。老师给了我们一个简单的练习:考虑以下简单的模式: Table Book: Column title (primary key) Column gen
我正在尝试学习 MVVM 模式,特别是当 View 表示数据库表时该怎么做,但 View 有几个元素表示单个数据库字段。举个简单的例子: 假设我有一个 DateTime 类型的数据库字段(每个数据库字
我有两张 table 。表单有约 77000 行。日志约有 270 万行。 以下查询将在不到一秒的时间内返回“30198”: SELECT COUNT(DISTINCT logs.DOCID) FRO
当您在 Eviews 中进行回归时,您会得到一组这样的统计数据: 在 R 中有没有一种方法可以在一个列表中获得所有/大部分关于 R 回归的统计数据? 最佳答案 请参阅summary,它将为大多数回归对
如果我枚举 type XType int const ( X1 XType = iota X2 ... Xn ) var XTypeNames = []string{"x1", "x2
我正在试用 ranger R包加速做了很多randomForest计算。我正在检查我从中得到的预测,并注意到一些有趣的事情,因为所做的预测完全不正确。 以下是比较 randomForest 的可重现示
我发现 Clang 编译速度比 GCC 慢了四倍。知道是什么原因造成的吗? ebg@tsuki(250)$ time /usr/bin/cc -DHC4 -DSAFETY -DNOREDUCE -DN
我注意到在尝试以 JSON 格式发布表单数据时,以下内容不起作用: $.ajax({ type: "POST", url: url, data: JSON.string
我的代码库中有很多 #if DEBUG/#endif 语句,它们大多具有断言类型逻辑,我不敢在生产环境中运行这些逻辑。 [Conditional("DEBUG")] public void Check
所以我正在开发一个平方根计算器,但我不知道 while 循环是否比 do while 循环更适合。 double x, y = 1.0, newY, squareRoot; bool
我有两个列表,一个是所有语言,另一个是网站拥有的语言子集,我的想法是返回所有语言,但如果子集的元素对应于所有语言的列表,则更改 bool 值的属性. 语言的DTO: public class DTOL
以下控制台应用程序运行正常 - 我很惊讶它没有出错。 class DelegateExperiments { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
我是一名优秀的程序员,十分优秀!