How Rendering Works: Overview | Lecture 123 | React.JS 🔥

The Coding Classroom
30 Nov 202306:55

Summary

TLDRThis video script provides an insightful overview of how React renders applications behind the scenes. It delves into the three phases involved in displaying components on the screen: the render phase, where React calls component functions and figures out how to update the DOM; the commit phase, where React actually updates the DOM; and the browser repaint phase, where visual changes are displayed. The script clarifies the distinction between React's definition of 'rendering' and the traditional understanding, emphasizing that rendering in React is an internal process without visual changes. It also explores how renders are triggered and the importance of understanding the entire rendering process for a comprehensive grasp of React's inner workings.

Takeaways

  • 🧩 React applications are built with components, which can be nested and instantiated multiple times.
  • 📄 JSX code in components produces React.createElement calls, which create React elements representing the UI.
  • 🌉 React elements are eventually transformed into DOM elements and rendered on the screen.
  • 🔄 React's rendering process is triggered by state updates or the initial application load.
  • 📖 The rendering process has three phases: render, commit, and browser repaint.
  • 🎨 In the render phase, React calls component functions to determine how to update the DOM, but doesn't actually update it.
  • ✏️ In the commit phase, React updates the DOM to reflect the latest state changes.
  • 💻 The browser then repaints the screen to display the visual changes.
  • ⏱️ Renders are not triggered immediately after state updates, but are scheduled when the JavaScript engine has free time.
  • 🔀 In some cases, like multiple setState calls in the same function, renders are batched together.

Q & A

  • What is the main purpose of this lecture series?

    -This lecture series aims to provide an in-depth understanding of how React renders applications behind the scenes. It is divided into three parts, with this video serving as an overview and the next two videos going into the React internals in greater detail.

  • What is the difference between a component and a component instance in React?

    -A component is a reusable piece of code that defines the structure and behavior of a part of the user interface. A component instance is the actual physical representation of a component in the application, holding its own state and props.

  • What role do React elements play in the rendering process?

    -React elements are the output of the JSX code, produced by calling the React.createElement function for each component instance. These React elements are then transformed into DOM elements and displayed as the user interface on the screen.

  • What is the purpose of the render phase in React?

    -In the render phase, React calls the component functions and figures out how to update the DOM to reflect the latest state changes. However, it does not actually update the DOM during this phase.

  • How does React's definition of rendering differ from the traditional understanding?

    -In React, rendering refers to the internal process of calling component functions and determining DOM updates, not the actual updating of the DOM or displaying elements on the screen. This differs from the traditional understanding of rendering as displaying elements visually.

  • When does the actual DOM update happen in React?

    -The actual DOM update happens in the commit phase, after the render phase. During the commit phase, new elements may be added to the DOM, existing elements may be updated, or elements may be removed to reflect the current state of the application.

  • What are the two ways in which a render can be triggered in React?

    -A render can be triggered in React in two ways: the initial render when the application first runs, and a re-render triggered by a state update in one or more component instances.

  • Does React trigger a render immediately after a state update?

    -No, React does not trigger a render immediately after a state update. Instead, the render is scheduled to occur when the JavaScript engine has some free time, usually just a few milliseconds later.

  • What is the significance of batching renders in React?

    -In some situations, such as multiple setState calls in the same function, React will batch renders together. This means that instead of triggering a render for each state update, React will combine them into a single render, improving performance.

  • What is the key takeaway from this lecture?

    -The key takeaway from this lecture is that the rendering process in React is more complex than it initially appears. It involves multiple phases, including the render phase, where component functions are called and DOM updates are determined, and the commit phase, where the actual DOM updates occur.

Outlines

00:00

🎬 React Rendering Process Overview

This paragraph serves as an overview of the React rendering process, which is split into three parts across this and the next two videos. It explains that as we build applications, we create components that produce React elements, which are ultimately transformed into DOM elements and displayed on the screen. While we understand how components are transformed into React elements, we don't yet understand how these React elements end up in the DOM and displayed on the screen, which is the focus of this series.

05:02

⚡️ Triggering a Render in React

This paragraph discusses how a render is triggered in React. There are two ways: 1) The initial render when the application first runs, and 2) A re-render triggered by a state update in one or more component instances. It's important to note that the render process is triggered for the entire application, not just the updated component. Renders are not triggered immediately after a state update, but rather scheduled when the JavaScript engine has free time. In some cases, like multiple setState calls in the same function, renders can be batched together.

Mindmap

Keywords

💡React

React is a JavaScript library for building user interfaces. It is the core subject of the video, which aims to explain how React renders applications behind the scenes. React is mentioned throughout the script as the technology being discussed and dissected.

💡Components

Components are the building blocks of React applications. The script mentions that when building applications, 'what we're really doing is building a bunch of components.' Components can be reused multiple times, and React creates instances of each component. Understanding how React handles components is crucial to grasping the rendering process.

💡JSX

JSX is a syntax extension for JavaScript used in React. The script states that 'each JSX will produce a bunch of React.createElement function calls, which in turn will produce a React element.' JSX is how developers write components in React, and it is eventually transformed into React elements during the rendering process.

💡React Elements

React elements are the objects that represent a component instance in React. The script explains that 'each JSX will produce a bunch of React.createElement function calls, which in turn will produce a React element.' These elements are then transformed into DOM elements and displayed on the screen.

💡Rendering

Rendering is a core concept in React, but as the script mentions, its definition in React is different from the traditional understanding. In React, rendering refers to the process of calling component functions and figuring out how to update the DOM to reflect state changes, but it does not actually involve updating the DOM or displaying elements on the screen.

💡Render Phase

The render phase is the first step in React's rendering process. During this phase, 'React calls our component functions and figures out how it should update the DOM.' However, as the script emphasizes, the DOM is not actually updated in this phase; it only determines what changes need to be made.

💡Commit Phase

The commit phase is the second step in React's rendering process, following the render phase. In this phase, 'new elements might be placed in the DOM, and already existing elements might get updated or deleted in order to correctly reflect the current state of the application.' This is the phase where the actual DOM updates occur, reflecting what was determined in the render phase.

💡State

State is a core concept in React, representing the internal data of a component. As the script mentions, 'state changes trigger renders,' meaning that when a component's state is updated, React will initiate the rendering process to reflect those changes in the user interface.

💡Initial Render

The initial render is the first time the React application is rendered. The script states that 'the first one is the very first time the application runs, which is what we call the initial render.' This is separate from subsequent re-renders triggered by state updates.

💡Re-render

A re-render is the process of rendering a React application again after the initial render. According to the script, a re-render occurs 'a state update happening in one or more component instances somewhere in the application.' Re-renders are triggered to reflect changes in the application's state and update the user interface accordingly.

Highlights

We are now ready to finally learn about how exactly React renders our applications behind the scenes.

As we build our applications, what we're really doing is building a bunch of components.

We then use these components inside other components, as many times as we want, which will cause React to create one or more component instances of each component.

As React calls each component instance, each JSX will produce a bunch of React.createElement function calls, which in turn will produce a React element for each component instance.

This React element will ultimately be transformed to DOM elements and displayed as a user interface on the screen.

We don't understand yet how these React elements actually end up in the DOM and displayed on the screen.

This process is started by React each time that a new render is triggered, most of the time by updating state somewhere in the application.

In the render phase, React calls our component functions and figures out how it should update the DOM to reflect the latest state changes, but it does not actually update the DOM in this phase.

React's definition of render is very different from what we usually think of as a render, which can be quite confusing.

In React, rendering is not about updating the DOM or displaying elements on the screen. Rendering only happens internally inside of React, and so it does not produce any visual changes.

Once React knows how to update a DOM, it does so in the commit phase, where new elements might be placed in the DOM, and already existing elements might get updated or deleted to reflect the current state of the application.

It's really this commit phase that is responsible for what we traditionally call rendering, not the render phase.

Finally, the browser will notice that the DOM has been updated and repaint the screen, which is the final step that actually produces the visual change that users see on their screens.

A render can be triggered by the very first time the application runs (initial render) or a state update happening in one or more component instances (re-render).

The render process is triggered for the entire application, not just for one single component, although that doesn't mean the entire DOM is updated.

Transcripts

play00:01

‫We are now ready

play00:02

‫to finally learn about how exactly React renders

play00:05

‫our applications behind the scenes.

play00:08

‫Now there is so much to learn here

play00:11

‫that I split up this lecture into three parts.

play00:14

‫So it's this video and the next two ones.

play00:17

‫This one serves more as an overview

play00:20

‫and then in the next two videos,

play00:21

‫we're gonna go really deep

play00:23

‫into some React internals.

play00:26

‫And let's start with just a small recap

play00:29

‫so that we're all on the same page.

play00:32

‫So as we build our applications,

play00:34

‫what we're really doing

play00:36

‫is building a bunch of components.

play00:39

‫We then use these components inside other components

play00:42

‫as many times as we want

play00:44

‫which will cause React to create one

play00:47

‫or more component instances of each component.

play00:51

‫So these are basically

play00:52

‫the actual physical components

play00:55

‫that live in our application

play00:57

‫and holds state and props.

play01:00

‫Now, as React calls each component instance,

play01:03

‫each JSX will produce a bunch

play01:06

‫of React dot create element function calls

play01:09

‫which in turn will produce a React element

play01:13

‫for each component instance.

play01:15

‫And so this React element

play01:17

‫will ultimately be transformed

play01:19

‫to DOM elements and displayed

play01:22

‫as a user interface on the screen.

play01:25

‫So we have a pretty good understanding

play01:28

‫of the initial part of this process,

play01:31

‫so of transforming components to React elements.

play01:36

‫However, what we don't understand yet

play01:38

‫is the second part of the process,

play01:41

‫so how these React elements actually end up

play01:44

‫in the DOM and displayed on the screen.

play01:48

‫Well, luckily for us,

play01:50

‫that is exactly what this series

play01:52

‫of lectures is all about.

play01:56

‫So in this lecture,

play01:58

‫we're gonna have a quick overview

play02:00

‫of each of the phases involved in displaying

play02:03

‫our components onto the screen.

play02:05

‫Then we're gonna zoom into each of these phases

play02:08

‫to learn how the entire process

play02:10

‫works internally behind the scenes.

play02:14

‫So this process that we're about to study

play02:16

‫is started by React each time

play02:19

‫that a new render is triggered,

play02:22

‫most of the time by updating state

play02:24

‫somewhere in the application.

play02:26

‫So state changes trigger renders

play02:29

‫and so it makes sense

play02:31

‫that the next phase is the render phase.

play02:34

‫In this phase, React calls our component functions

play02:38

‫and figures out how it should update the DOM,

play02:41

‫so in order to reflect

play02:43

‫the latest state changes.

play02:45

‫However, it does actually not update

play02:47

‫the DOM in this phase.

play02:50

‫And so Reacts definition of render

play02:52

‫is very different from what

play02:54

‫we usually think of as a render,

play02:57

‫which can be quite confusing.

play02:59

‫So again, in React, rendering is not

play03:03

‫about updating the DOM

play03:05

‫or displaying elements on the screen.

play03:08

‫Rendering only happens internally inside of React

play03:12

‫and so it does not produce any visual changes.

play03:16

‫Now, in all the previous sections,

play03:18

‫I have always used the term rendering

play03:21

‫with the meaning of displaying elements on the screen

play03:24

‫because that was just easy to understand

play03:27

‫and it just made sense, right?

play03:30

‫However, as we just learned,

play03:32

‫the rendering that I used to mean

play03:35

‫is really this render plus the next phase.

play03:39

‫And speaking of that next phase,

play03:41

‫once React knows how to update a DOM,

play03:44

‫it does so in the commit phase.

play03:48

‫So in this phase,

play03:49

‫new elements might be placed in the DOM

play03:52

‫and already existing elements might get updated

play03:55

‫or deleted in order to correctly reflect

play03:58

‫the current state of the application.

play04:01

‫So it's really this commit phase that is responsible

play04:05

‫for what we traditionally call rendering,

play04:08

‫not the render phase, okay?

play04:11

‫Then finally, the browser will notice

play04:14

‫that the DOM has been updated

play04:16

‫and so it repaints the screen.

play04:19

‫Now this has nothing to do with React anymore

play04:22

‫but it's still worth mentioning

play04:24

‫that it's this final step that actually produces

play04:27

‫the visual change that users see on their screens.

play04:32

‫All right, so let's now zoom

play04:34

‫into each of these different steps,

play04:37

‫starting with the triggering of a render.

play04:41

‫So there are only two ways

play04:43

‫in which a render can be triggered.

play04:46

‫The first one is

play04:47

‫the very first time the application runs

play04:50

‫which is what we call the initial render.

play04:53

‫And the second one is a state update happening

play04:57

‫in one or more component instances

play04:59

‫somewhere in the application

play05:01

‫which is what we call a re-render.

play05:05

‫And it's important to note

play05:06

‫that the render process really is triggered

play05:09

‫for the entire application,

play05:12

‫not just for one single component.

play05:15

‫Now that doesn't mean

play05:16

‫that the entire DOM is updated

play05:19

‫because remember, in React,

play05:21

‫rendering is only about calling the component functions

play05:25

‫and figuring out what needs to change in the DOM later.

play05:29

‫Now again, this might seem confusing now

play05:32

‫because earlier in the course,

play05:34

‫I made it seem as though React

play05:36

‫only re-renders the component

play05:39

‫where the state update happened.

play05:41

‫But that's because we were learning

play05:43

‫how React works in practice.

play05:46

‫And in fact, when we look at what happens in practice,

play05:50

‫it looks as if only

play05:52

‫the updated component is re-rendered.

play05:55

‫But now we are learning

play05:56

‫how React actually works behind the scenes.

play05:59

‫And so now we know that React looks

play06:02

‫at the entire tree whenever a render happens.

play06:06

‫Finally, I just want to mention

play06:08

‫that a render is actually not triggered immediately

play06:12

‫after a state update happens.

play06:14

‫Instead, it's scheduled for when

play06:17

‫the JavaScript engine basically

play06:19

‫has some free time on its hands.

play06:22

‫But this difference is usually

play06:23

‫just a few milliseconds

play06:25

‫that we won't notice.

play06:27

‫There are also some situations

play06:29

‫like multiple sets state calls

play06:31

‫in the same function where renders will be batched

play06:35

‫as we will explore a bit later.

play06:37

‫So this is how renders are triggered

play06:41

‫which is the easy part.

play06:43

‫What follows is the hard part,

play06:45

‫which is the actual rendering.

play06:48

‫And so let's learn all about that in the next video.

Rate This

5.0 / 5 (0 votes)

Related Tags
React InternalsComponent RenderingState ManagementDOM ReconciliationWeb DevelopmentJavaScript FrameworkTechnical TutorialProgramming ConceptsVisual ExplanationStep-by-Step Guide