رد سؤال المقابلة: ما الذي يتم عرضه في المتصفح ، أو المكون أو العنصر؟

** سؤال مخادع **

قد لا تعجبك الإجابة لأنها ، للأسف ، معقدة بعض الشيء.

أليست كلمة عنصر مرادفة لمكون الكلمة على أي حال ؟؟ تحديث: هذه المقالة هي الآن جزء من كتابي "React.js Beyond The Basics" . اقرأ النسخة المحدثة من هذا المحتوى والمزيد حول React على jscomplete.com/react-beyond-basics .

من الناحية الفنية ، لا يعرض ReactDOM مكون React أو عنصر React في DOM. يعرض عناصر DOM مدعومة بنماذج من مكوناتها . هذا صحيح بالنسبة لمكونات الفصل. بالنسبة لمكونات الوظيفة ، يعرض ReactDOM عناصر DOM فقط. لا تحتوي مكونات الوظيفة على مثيلات (يمكن الوصول إليها باستخدام this) لذلك عند استخدام مكون دالة ، يعرض ReactDOM عنصر DOM تم إنشاؤه من العنصر الذي تم إرجاعه للوظيفة.

ما تحتاج إلى فهمه هنا هو أن عنصر React يختلف عن عنصر DOM. عنصر React هو مجرد وصف لعنصر HTML أو مكون React أو مزيج من هذه العناصر.

حسنًا ، قد يكون سؤال المقابلة الأفضل هو: عندما تستخدم شيئًا مثل /> 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