Pitanje za reakcijski intervju: Što se prikazuje u pregledniku, komponenti ili elementu?

** Trik pitanje **

Odgovor vam se možda neće svidjeti jer je, nažalost, malo kompliciran.

Nije li element riječi ionako sinonim za komponentu riječi ?? Ažuriranje: Ovaj je članak sada dio moje knjige "React.js izvan osnova". Pročitajte ažuriranu verziju ovog sadržaja i više o Reactu na jscomplete.com/react-beyond-basics .

Tehnički gledano, ReactDOM ne generira React komponentu ili React element u DOM-u. Renderira DOM elemente potkrijepljene primjercima njihovih komponenata . To vrijedi za komponente klase. Za funkcionalne komponente, ReactDOM prikazuje samo DOM elemente. Funkcijske komponente nemaju instance (kojima se može pristupiti this), pa kad koristi funkcijsku komponentu, ReactDOM generira DOM element generiran iz vraćenog elementa funkcije.

Ovdje trebate razumjeti da se React element razlikuje od DOM elementa. React element je samo opis HTML elementa, React komponente ili njihove kombinacije.

U redu, bolje pitanje za intervju moglo bi biti: Kada koristite nešto poput /> in JSX, is that a component, an element, or an ins tance?

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 reactjs.org slightly modified to have a function component as well:

const Today = () => ( Today is {new Date().toDateString()} );
class HelloMessage extends React.Component { render() { return ( Hello {this.props.name} ); }}
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