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!
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.
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.
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.
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.
Some simplified definitions first:
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.dom
manipulations are costly. Either in time or resourcesvirtual 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:
render
methods to populate its virtual dom
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:
render
method is returning the same thing, react will do nothing on the screen. render
method multiple times, even when you don't expect it to. 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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With