Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling render vs painting the DOM?

I’m learning about React and trying to get a better understanding about the lifecycle and about the different stages.

One thing I just read states “React first renders and then mounts elements. Rendering in this context means calling a class’s render(), not painting the DOM”

I guess I just don’t really get what that means. Can someone explain it in a simple way or with examples?

Thanks in advance!

like image 578
CustardBun Avatar asked Jan 25 '18 07:01

CustardBun


People also ask

What does rendering to the Dom mean?

Javascript uses the document object model (DOM) to manipulate the DOM elements. Rendering refers to showing the output in the browser. The DOM establishes parent-child relationships, and adjacent sibling relationships, among the various elements in the HTML file.

What is painting in React?

Browser uses dom to decide what to display. In react, this is referred to as painting, because whenever something is added to a dom, browser has to re-paint the screen.

What is the difference between mounting and rendering?

Introduction. Render is a class-based component that gets called and returns the set of instructions for creating DOM. Mounting is when the component renders first time and indeed builds the initial DOM from that instruction.

When render method is called?

render() 101 First of all, render() is not user callable. It is part of the React component lifecycle. Generally, it gets called by React at various app stages when the React component instantiates for the first time, or when there is a new update to the component state.


1 Answers

Some simplified definitions first:

  1. Browser uses dom to decide what to display. In react, this is referred to as painting, because whenever something is added to a dom, browser has to re-paint the screen.
  2. dom manipulations are costly. Either in time or resources
  3. One of the reasons react looks so fast is that it uses something people call a virtual dom. Virtual dom tries to mirror the actual dom, they might be out of sync for a while, as all changes to virtual dom are not reflected on the actual dom immediately (else what would be the benefit of having a virtual dom?)

With these definitions in mind, let us take a look at what React does:

  1. When react will fire render methods to populate its virtual dom
  2. It will keep batching the changes to the virtual dom, and once it determines a good time to change the screen, it will flush only the changes to the dom. This is called painting.

React uses a lot of optimization under the hood, I don't know all of them (would also argue, that I don't need to know what they do), but here are somethings to remember:

  1. As long as your render method is returning the same thing, react will do nothing on the screen.
  2. It is thus expected that react may call render method multiple times, even when you don't expect it to.
  3. It is thus recommended to keep your render method as free of any side effects or any resource intensive operations, such as fetching data, or transforming your data structure. Think of it as: render method should only have logic related to what needs to be rendered, anything else that it may need to do that, can be calculated outside and put in state or variables or even memoized.
like image 125
dubes Avatar answered Sep 27 '22 22:09

dubes