Instances and Elements in Practice | Lecture 122 | React.JS 🔥

The Coding Classroom
30 Nov 202307:54

Summary

TLDRThis video script delves into the intricacies of component instances and React elements in React code. It explores the significance of logging a component instance to the console, highlighting its type and props. The concept of directly calling components versus rendering them in JSX is discussed, emphasizing the importance of adhering to proper rendering practices to maintain component state management and avoid potential security risks. The script also touches on React's built-in protection against cross-site scripting attacks through the use of symbols. Overall, it provides insights into the inner workings of React components and the importance of following best practices for efficient and secure code implementation.

Takeaways

  • 👉 React internally calls a component function and returns a React element representation when rendering a component.
  • 🧐 The React element representation contains the component type and props, and uses a symbol for security against cross-site scripting attacks.
  • ⚠️ Directly calling a component function yourself will not create a proper component instance, and React will treat the result as a raw React element instead.
  • 🔑 Always render components using JSX, allowing React to properly handle component instances and manage their state.
  • 🌳 Component instances each have their own state and props, separate from the parent component.
  • 🚫 Calling components directly within another component violates the rules of hooks and can lead to issues with state management.
  • 💡 The provided example demonstrates rendering a single component blueprint multiple times, creating separate component instances.
  • 🚀 React's component rendering process involves creating instances from the component blueprint, managing state, and rendering the appropriate elements.
  • 🔒 The symbol used in React elements prevents cross-site scripting attacks by ensuring the element cannot be transmitted via JSON from an untrusted source.
  • 📐 Logging a component instance to the console shows its type and props, while directly calling the component function shows the raw React element output.

Q & A

  • What is the purpose of logging the component instance to the console?

    -The purpose of logging the component instance to the console is to inspect the internal structure and properties of the component as seen by React. This allows developers to understand how React represents and processes components internally.

  • What is the significance of the '$$ type' symbol in the component instance output?

    -The '$$ type' symbol is a security feature implemented by React to protect against cross-site scripting attacks. Since symbols are JavaScript primitives that cannot be transmitted via JSON, React can identify and reject fake React elements that may be sent from an API, preventing potential security vulnerabilities.

  • Why is it not recommended to call components directly instead of rendering them through JSX?

    -Calling components directly instead of rendering them through JSX can lead to several issues. React may not recognize the component as an instance, which can cause problems with state management and violate the rules of hooks. Additionally, direct component calls bypass React's internal rendering process, which can lead to unexpected behavior and potential performance issues.

  • What happens when a component is called directly within another component's code?

    -When a component is called directly within another component's code, React does not recognize it as a component instance. Instead, it treats the output as a raw React element. This means that the called component cannot manage its own state, and its state and hooks may get merged into the parent component's state, violating the rules of hooks and potentially causing other issues.

  • How does the script demonstrate the difference between a component instance and a raw React element?

    -The script logs both a component instance and a directly called component to the console. The component instance output shows the component type and any passed props, while the directly called component output shows a different type (the rendered content) and different props (like the class name).

  • What is the recommended way to render a component in React?

    -The recommended way to render a component in React is by using JSX syntax, which allows React to recognize it as a component instance and properly handle its rendering, state management, and lifecycle methods.

  • What is the relationship between a component and its instances in React?

    -A component in React is like a blueprint or a template, while component instances are the actual rendered instances of that component. Each instance has its own state and props, allowing for multiple instances of the same component to exist with different data and behavior.

  • How does the script demonstrate the concept of multiple component instances?

    -The script shows that the 'TabContent' component is rendered four times, resulting in four separate component instances. Each instance has its own state and props, as demonstrated by the component tree inspection in the script.

  • What is the purpose of the 'TabContent' component used in the script?

    -The 'TabContent' component is a stateful component used to manage the content displayed within a tab interface. It likely contains state and logic related to rendering and updating the content based on user interactions or other events.

  • What is the overall message or lesson conveyed by the script?

    -The overall message conveyed by the script is the importance of following the recommended practices for rendering and working with components in React. It highlights the potential issues that can arise from directly calling components instead of rendering them through JSX, and emphasizes the need to respect React's internal rendering process and component instance model.

Outlines

00:00

🧪 Exploring Component Instances and React Elements

This paragraph explains how to examine component instances and React elements in code. It demonstrates logging a component to the console, showing its type, props, and the effect of calling a component directly instead of rendering it through JSX. It also explains React's security feature involving symbols to protect against cross-site scripting attacks.

05:01

⚠️ Pitfalls of Directly Calling Components

This paragraph further illustrates the consequences of directly calling a component instead of rendering it through JSX. It shows that doing so causes React to no longer recognize it as a component instance and instead treats it as a raw React element. It also demonstrates how this violates the rules of hooks by moving the component's state into the parent component's state. The paragraph emphasizes the importance of always rendering components through JSX to avoid these issues.

Mindmap

Keywords

💡Component Instance

A component instance refers to an individual rendered copy of a React component. In the video, the instructor demonstrates that when a component is rendered in JSX, React internally calls the component function and creates a component instance. Each instance has its own state and props, allowing multiple instances of the same component to coexist with different data. The video highlights the importance of rendering components through JSX to ensure React recognizes them as instances.

💡React Element

A React element is an object representation of a component or DOM node. It describes what should be rendered on the screen. In the video, the instructor shows that when a component is rendered, React internally returns a React element. This element contains information about the component type, props, and other metadata. React uses these elements to create and update the actual DOM nodes. The video also explains the security feature of React elements, where a symbol is used to prevent cross-site scripting attacks.

💡JSX

JSX (JavaScript XML) is a syntax extension for JavaScript that allows developers to write HTML-like code within their JavaScript files. In React, JSX is used to define the structure and content of components. The video emphasizes the importance of rendering components within JSX, as this is how React recognizes them as component instances and creates the appropriate React elements. Writing JSX directly within the code, as shown in the video, allows React to handle the rendering process correctly.

💡Props

Props (short for properties) are input data passed from a parent component to a child component in React. In the video, the instructor demonstrates how to pass props to a component instance by including key-value pairs within the JSX. The props are then accessible within the child component's function, allowing for dynamic rendering and behavior based on the received data. The video highlights how props are included in the generated React element for each component instance.

💡State

State is an internal data store managed by a React component. It holds information that can influence the component's rendering and behavior. In the video, the instructor shows that each component instance has its own state, separate from other instances. When a component is improperly rendered outside of JSX, its state becomes part of the parent component's state, causing issues with state management and hooks. The video emphasizes the importance of rendering components within JSX to ensure proper state management.

💡Hooks

Hooks are functions in React that allow developers to use state and other React features in functional components. The video briefly mentions hooks when demonstrating the improper rendering of a component outside of JSX. In this case, the hooks from the child component are merged into the parent component's state, violating the rules of hooks. The video emphasizes the importance of rendering components within JSX to avoid such issues with hooks.

💡Component Tree

The component tree is a hierarchical representation of the components in a React application. In the video, the instructor uses the React DevTools to examine the component tree and highlight the issues that arise when components are rendered improperly outside of JSX. The component tree helps visualize the parent-child relationships between components and their instances, which is crucial for understanding the flow of data and state management.

💡Cross-Site Scripting (XSS)

Cross-Site Scripting (XSS) is a type of security vulnerability that allows attackers to inject malicious code into web applications. In the video, the instructor explains that React uses a symbol in its React elements to protect against XSS attacks. Symbols cannot be transmitted via JSON, which means that if a hacker tries to send a fake React element through an API, React will not recognize it as a valid element and will not render it, preventing the potential attack.

💡Blueprint

In the context of the video, the term "blueprint" is used to refer to the component definition or the component function itself. The instructor likens the component to a blueprint, as it serves as the template or instructions for creating instances of that component. Each instance is then rendered with its own state and props, while adhering to the blueprint defined by the component function.

💡Render

To render in React means to create or update the visual representation of a component on the screen. Throughout the video, the instructor emphasizes the importance of rendering components correctly within JSX to ensure that React recognizes them as component instances and handles the rendering process appropriately. Improper rendering, such as calling a component function directly, can lead to issues with state management, hooks, and the overall functionality of the application.

Highlights

React internally calls the component function when rendering components, and returns a React element.

The React element returned from a component function includes the component's name as the type and any props passed to the component.

React uses a symbol type to prevent cross-site scripting attacks by ensuring that React elements cannot be passed from an external API.

Calling a component function directly, instead of rendering it through JSX, results in React treating the output as a raw React element rather than a component instance.

When a component is called directly, React does not recognize it as a component instance, and the component cannot manage its own state or hooks properly.

Calling components directly violates the rules of hooks and should be avoided.

Components should always be rendered through JSX, allowing React to recognize them as component instances and manage their state and lifecycle correctly.

In a component tree, a single component definition can have multiple component instances, each with its own state and props.

The transcript provides a practical demonstration of rendering components directly versus through JSX, highlighting the differences in how React treats each case.

The transcript emphasizes the importance of following best practices when working with React components to ensure proper state management and avoid potential security vulnerabilities.

The transcript explains the purpose and significance of the symbol type used by React to identify genuine React elements.

The transcript highlights the distinction between component definitions (blueprints) and component instances, and how React treats each differently.

The transcript provides insights into React's internal workings and how it renders components and manages their state.

The transcript demonstrates the potential issues that can arise when rendering components directly, such as state management problems and violation of React's rules.

The transcript emphasizes the importance of following React's recommended practices for rendering components through JSX to ensure proper functionality and avoid potential security risks.

Transcripts

play00:01

‫So as promised, let's now shortly look

play00:04

‫at component instances and React elements in our code.

play00:10

‫So what I want to do in this lecture is just a couple

play00:13

‫of quick experiments to show you some interesting things.

play00:18

‫So first off, we can actually look

play00:20

‫at a component instance simply

play00:23

‫by using the component and logging it to the console.

play00:27

‫So let's try that out.

play00:29

‫So actually right out here we can write JSX like this.

play00:36

‫So, let's use the different component

play00:39

‫or the different content component

play00:41

‫because this one doesn't have any state.

play00:45

‫And all right, and if we run this

play00:47

‫then we should see something in the console.

play00:51

‫And indeed, there it is.

play00:53

‫So as soon as React sees this right here,

play00:57

‫it will internally call the different content function

play01:01

‫and will then return this React element.

play01:04

‫So just like we learned in the previous video.

play01:07

‫So let's take a quick look at this

play01:11

‫and while this is not really interesting, we can see

play01:14

‫that the type here is of different content.

play01:17

‫And so that's exactly the name of the component right here.

play01:21

‫We can also see that we didn't pass in any props

play01:24

‫but which we actually could.

play01:27

‫So let's just do test equals 23.

play01:31

‫and so then we should see that right here.

play01:34

‫So now in the second one, and indeed there it is.

play01:38

‫So again, this is what React internally use

play01:41

‫to then later create our dumb elements.

play01:45

‫Now if you're wondering what this weird dollar dollar type

play01:48

‫of thing here is, well this is simply a security feature

play01:53

‫that React has implemented in order to protect us

play01:56

‫against cross-site scripting attacks.

play02:00

‫So, notice how this is a symbol and symbols are one

play02:04

‫of the JavaScript primitives, which cannot be transmitted

play02:09

‫via JSON, or in other words, this means that a symbol

play02:13

‫like this cannot come from an API call.

play02:17

‫So if some hacker would try to send us a fake React

play02:20

‫element from that API, then React would not see

play02:25

‫this type of as a symbol.

play02:28

‫Again, because symbols cannot be transmitted via JSON.

play02:32

‫And so then React would not include that fake React element

play02:36

‫into the dumb so protecting us against that kind of attack.

play02:41

‫All right, but anyway, let's come back here

play02:45

‫and try something else.

play02:47

‫So, if React calls are component internally

play02:51

‫when it renders them, so just as it did here in this

play02:55

‫previous line, then maybe you have wondered,

play02:58

‫why don't we just call components directly?

play03:01

‫So why should we write it like this when

play03:04

‫we could also write different content like this?

play03:12

‫So, basically calling the function ourselves.

play03:15

‫Well, there is really nothing stopping us from doing so.

play03:19

‫So if we save this, then we actually get a result as well.

play03:24

‫Let's just reload here so that we only get

play03:26

‫these two outputs.

play03:28

‫So one output from here and one from here.

play03:32

‫So you see that even here in this case,

play03:35

‫we still got a React element like this.

play03:38

‫However, it is a very different one.

play03:41

‫So this one no longer has the type of different content

play03:45

‫and instead it is a diff which is basically

play03:49

‫just the content of that component.

play03:52

‫So this diff here is now the type of this React element

play03:56

‫and we can also see that because the props include

play03:59

‫the class name of "tab-content".

play04:02

‫So what this means is that right now, React does

play04:06

‫no longer see this as a component instance,

play04:10

‫and instead it just sees the raw React element,

play04:14

‫which is really not what we want.

play04:16

‫So, when we write, or actually when we use a component,

play04:20

‫we want React to see the component instance

play04:24

‫and not the raw output element like this one.

play04:29

‫So never do this, what we did right here,

play04:33

‫and let's actually demonstrate this one more time,

play04:36

‫but now inside the component here.

play04:39

‫So after all this, let's enter JavaScript mode here

play04:45

‫and then let's just call or actually tab content.

play04:51

‫This time, let's call tab content

play04:54

‫and we can pass in props just like this.

play04:57

‫So we pass in an object and then item,

play05:01

‫and then let's say content at zero.

play05:07

‫And well, we got some errors here, but let's try to reload.

play05:13

‫And now this somehow works.

play05:16

‫So you see that actually

play05:18

‫we got a second tab content rendered down here.

play05:22

‫So, it looks like this works, right?

play05:26

‫Well, not so fast.

play05:28

‫Let's check out our component tree here again.

play05:34

‫We need some more space.

play05:36

‫And so now you see that we still only have one

play05:40

‫tab content here in our component tree.

play05:43

‫And so this happened exactly because of what I explained

play05:45

‫before, which is that when we call a component directly

play05:49

‫like this, then React no longer sees it

play05:52

‫as a component instance.

play05:55

‫Now we can also see that the state

play05:57

‫that this component manages is actually now

play06:01

‫inside the parent state or the parent component.

play06:05

‫So, if we check out the tabbed component here,

play06:09

‫you see that it has the normal state that it had before

play06:13

‫which is this active tab

play06:15

‫but it also has some other hooks here which come

play06:19

‫from tab content.

play06:20

‫So these two are the two hooks that are inside

play06:24

‫this component, but they don't really belong here.

play06:27

‫So we actually want them to be inside tab content

play06:31

‫and not inside, well not here inside the tabbed component.

play06:36

‫So what this means again, is that this is here

play06:40

‫not really a component because it cannot even manage

play06:43

‫its own state at this point.

play06:46

‫So, for all these reasons, you should never ever do

play06:50

‫what we just did here because this will create

play06:53

‫multiple problems such as violating the rules of hooks

play06:57

‫that we will talk about later.

play06:59

‫So instead, as you already know,

play07:01

‫always render it inside the JSX.

play07:04

‫So just like this.

play07:06

‫And so here we just use the component.

play07:09

‫So, basically this blueprint like we have always been doing.

play07:13

‫And so then React calls the component

play07:15

‫and actually recognizes this as a component instance.

play07:20

‫Alright, so let's reload here again

play07:26

‫and then we are back to normal.

play07:31

‫Alright, and here we can see one of those examples

play07:35

‫that we talked about in the previous lecture.

play07:38

‫So, we wrote one tab component here.

play07:42

‫So this is like the blueprint

play07:43

‫but then we have four component instances,

play07:47

‫each with its own state and its own props.

Rate This

5.0 / 5 (0 votes)

Related Tags
ReactComponentsRenderingCode ExamplesBest PracticesJavaScriptFront-End DevelopmentWeb DevelopmentTutorialCoding