在 React 中通过 Javascript 修改元素的属性值是个好主意吗?

问题描述

既然 React 有自己的 DOM 副本,那么在不影响 React 的虚拟 DOM(性能)的情况下修改 HTML 元素的属性的最佳方法是什么?

假设我想根据 active 的可见性(Intersection Observer)在从 div 列表中添加删除 if (inView) { element.classList.add('active') } else { element.classList.remove('active') } 类之间切换

在 Javascript 中我可以做到:

# read both files
with open('file_one.txt') as f:
    file_one = f.read().splitlines()

with open('file_two.txt') as f:
    file_two = f.read().splitlines()

# method 1 by stephan berger:
result = list(set(file_one)^set(file_two))

# method 2:
for idx_one,line_one in enumerate(file_one):
    for idx_two,line_two in enumerate(file_two):
        if line_two == line_one:
            print(f"Removing duplicate: {line_two}")
            file_one.pop(idx_one)

# write new file
with open("output.txt","w") as f:
    for line in result:  # file_one for method 2
        f.write(line + "\n")
print("Generated output.txt")

这会影响 React 的虚拟 DOM 吗?如果是,你能提出更好的方法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...