简体   繁体   English

Reactjs 如何仅更新 html dom 元素中的部分文本?

[英]How can Reactjs update only part of text in a html dom element?

I am learning reactjs and stumbled upon this peculiar detail, see my image below.我正在学习 reactjs 并偶然发现了这个特殊的细节,请参见下面的图片。 It is the react demo showing how reactjs only update the parts of the dom which have changed.这是反应演示,展示了 reactjs 如何仅更新已更改的 dom 部分。

来自浏览器检查视图的特殊屏幕截图

function tick() {
  const element = (
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {new Date().toLocaleTimeString()}.</h2>
    </div>
  );
  // highlight-next-line
  ReactDOM.render(element, document.getElementById('root'));
}

setInterval(tick, 1000);

Live example: https://codepen.io/pen?&editors=0010现场示例: https://codepen.io/pen?&editors=0010

As you can see, when the clock ticks, only the second part of the text gets updated.如您所见,当时钟滴答作响时,只有文本的第二部分得到更新。 In my experience, I can update the content of a single html-element, meaning my code would update the entire text of the h2-element.根据我的经验,我可以更新单个 html 元素的内容,这意味着我的代码将更新 h2 元素的整个文本。 Here it really looks like there are three text elements inside the h2-element.在这里,看起来 h2 元素内确实有三个文本元素。 I must be missing some basic knowledge here!我一定在这里遗漏了一些基本知识!

As it looks, it seems to be possible to address/find and update a sub-text as if it is an element of its own, in this case, the "3:05:42 PM" text piece.看起来,似乎可以寻址/查找和更新子文本,就好像它是它自己的元素一样,在这种情况下,“下午 3:05:42”文本片段。 How do you do this in plain javascript?你如何在普通的 javascript 中做到这一点?

How React does this specifically is a question for the documentation and the code itself, however we can have a look at how we might do this in plain JS with a simulation such as this one: React 如何具体做到这一点是文档和代码本身的一个问题,但是我们可以看看我们如何在普通的 JS 中通过类似这样的模拟来做到这一点:

let t1 = document.createTextNode("The seconds are: ");
let t2 = document.createTextNode(new Date().getSeconds());
let t3 = document.createTextNode(". Isn't it cool?");
    
const content = document.getElementById("content");

content.appendChild(t1);
content.appendChild(t2);
content.appendChild(t3);

setInterval(() => {
  let nextT2 = document.createTextNode(new Date().getSeconds());
  content.replaceChild(nextT2, t2);
  t2 = nextT2;
}, 1000);

https://codesandbox.io/s/replace-text-node-h4ivy https://codesandbox.io/s/replace-text-node-h4ivy

The idea here is that we're able to create the text as separate text nodes which we can later replace directly.这里的想法是我们能够将文本创建为单独的文本节点,我们以后可以直接替换它们。 In the case of React, they could - if they so chose to - create each part of the text as a separate text node.在 React 的情况下,如果他们愿意的话,他们可以将文本的每个部分创建为单独的文本节点。 React will already know that the content is in 3 parts (text, code-block, text), so it seems reasonable to conclude that they may choose to append it to the DOM as 3 text nodes. React 已经知道内容分为 3 个部分(文本、代码块、文本),因此可以合理地得出结论,他们可能会选择 append 将其作为 3 个文本节点到 DOM 中。

Our setInterval here is representing the final stages of some process that eventually updates the DOM.我们这里的setInterval代表最终更新 DOM 的某个过程的最后阶段。 If you check in the dev tools you'll probably see only the numbers updating, similar to how you see it updating in React.如果您签入开发工具,您可能只会看到数字更新,类似于您在 React 中看到的更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM