Question d'entrevue React: Qu'est-ce qui est rendu dans le navigateur, un composant ou un élément?

** Question piège **

Vous n'aimerez peut-être pas la réponse car, malheureusement, c'est un peu compliqué.

Le mot élément n'est-il pas de toute façon synonyme du mot composant ?? Mise à jour: Cet article fait maintenant partie de mon livre «React.js Beyond The Basics». Lisez la version mise à jour de ce contenu et plus sur React sur .

Techniquement parlant, ReactDOM ne rend pas un composant React ou un élément React dans le DOM. Il restitue les éléments DOM soutenus par des instances de leurs composants . Cela est vrai pour les composants de classe. Pour les composants de fonction, ReactDOM ne rend que les éléments DOM. Les composants de fonction n'ont pas d'instances (auxquelles on peut accéder avec this) donc lors de l'utilisation d'un composant de fonction, ReactDOM restitue un élément DOM généré à partir de l'élément retourné par la fonction.

Ce que vous devez comprendre ici, c'est qu'un élément React est différent d'un élément DOM. Un élément React est simplement une description d'un élément HTML, d'un composant React ou d'un mélange de ceux-ci.

D'accord, une meilleure question d'entrevue pourrait être: lorsque vous utilisez quelque chose comme /> in JSX, is that a component, an element, or an instance?

It’s an element but not a DOM element. It’s a React element. The clue here is that any JSX tag gets translated to a React.createElement call. Keep that in mind. CREATE. ELEMENT.

However, for React to continue working with this React element, it will have to either invoke a function or create an instance from a class.

You might find the words component, element, and instance mixed up in the React guides and tutorials out there. I am guilty of mixing these words myself, but I think a beginner React learner needs to understand the important distinctions. The React blog has a post about this topic but that I think it is a bit too technical for a beginner.

Here is how I would provide simple definitions of these word to beginners:

  • A React Component is a template. A blueprint. A global definition. This can be either a function or a class (with a render function).
  • A React Element is what gets returned from components. It’s an object that virtually describes the DOM nodes that a component represents. With a function component, this element is the object that the function returns. With a class component, the element is the object that the component’s render function returns. React elements are not what we see in the browser. They are just objects in memory and we can’t change anything about them.
  • React internally creates, updates, and destroys instances to figure out the DOM elements tree that needs to be rendered to the browser. When working with class components, it’s common to refer to their browser-rendered DOM elements as component instances. You can render many instances of the same component. The instance is the “this” keyword that you use inside class-based components. You would not need to create an instance from a class manually. You just need to remember that it’s there somewhere in React’s memory.
  • Function-based React elements do not have instances. A function component can still be rendered multiple times but React just does not associate a local instance with each render. It just uses the invocation of the function to determine what DOM element to render for the function.

The bottom line is that ReactDOM does not render components in the browser, and it does not render elements either (in the sense of keeping the term element to represent the result of React.createElement). It also does not render instances. It renders DOM elements.

Unfortunately, it seems to be a common practice out there to use the term component to mean both the template and any instances or invocations used though the template. I don’t blame anyone for being confused here. This is a bit painful.

What’s the story here?

Every React App starts with a render call that uses a React element. Let’s use the HelloMessage example from slightly modified to have a function component as well:

const Today = () => ( Today is {new Date().toDateString()} );
class HelloMessage extends React.Component { render() { return ( Hello {} ); }}
ReactDOM.render( , mountNode);

The first React element is the one we start with in the ReactDOM.render call:

 // This is a React element

This React element describes that the DOM tree to be rendered should start with the HelloMessage component and a name prop value that’s equal to Taylor.

React now needs to answer the question: What is HelloMessage?

Every time a React element describes a React component (like the React element we have above), React uses the component to replace that description with what the component returns. It creates an instance for class-based components at this point and keeps a reference of that in memory. It does not create anything for function-based components; it just invokes them.

What gets returned from the HelloMessage component is a React element that describes a React.Fragment component.

React now needs to answer the question: What is React.Fragment?

React will keep reducing these unknown descriptions of components until it has only valid DOM nodes. The description of React.Fragment gets translated into 2 React elements, one describing a div and another describing a Today component.

React now needs to answer the question: What is Today?

It calls the Today function to figure this last question out. The Today function returns a React element that describes a div.

At this point, the virtual tree is complete with all React elements that describe DOM nodes. React uses its reconciliation algorithm to figure out what to update in the browser. The nodes that were translated with a component instance retain the power of modifying that instance.

Did this clear things up a bit or did I confuse the terms a bit more? Let me know in the responses below.

Thanks for reading.

Learning React or Node? Checkout my books:

  • Learn React.js by Building Games
  • Node.js Beyond the Basics

Original text