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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…