Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

javascript - Updating the Rendered Element(immutable)

In the React tutorial , It says

React elements are immutable. Once you create an element, you can’t change its children or attributes. An element is like a single frame in a movie: it represents the UI at a certain point in time. With our knowledge so far, the only way to update the UI is to create a new element and pass it to ReactDOM.render().

In the Next Heading, It says

React only updates What's necessary

React DOM compares the element and its children to the previous one, and only applies the DOM updates necessary to bring the DOM to the desired state.

Example took by them -

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

setInterval(tick, 1000);

In this example React Only updates the time - <h2>It is {new Date().toLocaleTimeString()}.</h2> line of the code. Because this is only necessary changes but I couldn't be able to understand how React changing the Immutable object as they have mentioned

React elements are immutable. Once you create an element, you can’t change its children or attributes.

So rather than only changing the "Just Time Part" (of above code example), It should change whole React Element. I couldn't be able to understand how could React does necessary updates inside the Immutable object (in above case it is the element) or I'm missing something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It doesn't do updates on React Element Tree ('the immutable object'). It compares previous tree with the current one and does necessary updates to the DOM.

React Element Tree is a simplified form of the DOM. It's like a snapshot. React has the current snapshot and when the state of an application changes, it creates a new state that reflects how the DOM should look like. React compares those two snapshots and makes required changes to the DOM so that it mirrors the new snapshot. After that the old, outdated snapshot is trashed and the new one becomes the current snapshot of the DOM.

Basically, you have:

  • the state of an app
  • description how an app should look like for a given state (the React code you write)
  • snapshot (React Element Tree)
  • Diffing and updating machinery (React library)
  • DOM

DOM or external world (i.e. server) produce events that change the state. A new snapshot is created for that state based on the description. Old and new snapshots are compared and changes are introduced to the DOM. This process repeats over and over again.

You can see and learn more about React elements in this fantastic blog post: https://reactjs.org/blog/2015/12/18/react-components-elements-and-instances.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...