react控制組件中元素_React Interview問題:瀏覽器,組件或元素中呈現了什么?

react控制組件中元素

by Samer Buna

通過Samer Buna

React Interview問題:瀏覽器,組件或元素中呈現了什么? (React Interview Question: What gets rendered in the browser, a component or an element?)

**技巧問題** (** Trick Question **)

You might not like the answer because, unfortunately, it is a bit complicated.

您可能不喜歡答案,因為不幸的是,它有點復雜。

Isn’t the word element synonymous to the word component anyway??

不字元素同義字部件反正?

Update: This article is now part of my book “React.js Beyond The Basics”.

更新:本文現在是我的書《超越基礎的React.js》的一部分。

Read the updated version of this content and more about React at jscomplete.com/react-beyond-basics.

jscomplete.com/react-beyond-basics中閱讀此內容的更新版本以及有關React的更多信息

Technically speaking, ReactDOM does not render a React component or a React element in the DOM. It renders DOM elements backed by instances of their components. This is true for class components. For function components, ReactDOM renders just DOM elements. Function components don’t have instances (that can be accessed with this) so when using a function component, ReactDOM renders a DOM element generated from the function’s returned element.

從技術上講,ReactDOM不會在DOM中呈現React組件或React元素。 它呈現由其組件實例支持的DOM元素 。 對于類組件,這是正確的。 對于功能組件,ReactDOM僅呈現DOM元素。 函數組件沒有實例(可以使用this進行訪問),因此在使用函數組件時,ReactDOM會呈現從函數的返回元素生成的DOM元素。

What you need to understand here is that a React element is different from a DOM element. A React element is just a description of an HTML element, a React component, or a mix of these.

您需要在這里理解的是,React元素與DOM元素不同。 甲陣營元件僅僅是一個HTML元素的描述 ,一個陣營成分,或這些的混合物。

Okay, a better interview question might be: When you use something like <MyComponent /> in JSX, is that a component, an element, or an instance?

好的,一個更好的面試問題可能是: 當您在JSX中使用<MyComponent />之類的東西時,是組件,元素還是實例?

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.

這是一個元素,但不是DOM元素。 這是一個React元素。 這里的線索是,所有JSX標簽都將轉換為React.createElement調用。 記在腦子里。 創造。 元素

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.

但是,要讓React繼續使用此React元素,它必須調用一個函數或從一個類創建一個實例。

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.

在React指南和教程中,您可能會發現單詞componentelementinstance混合在一起。 我自己混用這些單詞是有罪的,但是我認為React的初學者需要了解重要的區別。 React博客上有一篇關于該主題的文章,但是我認為對于初學者來說,它有點技術性。

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).

    React Component是一個模板。 藍圖。 全局定義。 這可以是一個函數或一個 (帶有渲染函數)。

  • 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 Element是從組件返回的內容。 這個對象實際上描述了組件代表的DOM節點。 對于函數組件,此元素是函數返回的對象。 對于類組件,元素是組件的渲染函數返回的對象。 React元素不是我們在瀏覽器中看到的。 它們只是內存中的對象,我們無法對其進行任何更改。

  • 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.

    React在內部創建,更新和銷毀實例,以找出需要呈現給瀏覽器的DOM元素樹。 在使用類組件時,通常將其瀏覽器呈現的DOM元素稱為組件實例。 您可以呈現同一組件的許多實例。 實例是您在基于類的組件中使用的“ this ”關鍵字。 您無需手動從類創建實例。 您只需要記住它就在React的內存中。

  • 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.

    基于函數的React元素沒有實例。 一個功能組件仍然可以多次渲染,但是React不會將本地實例與每個渲染關聯。 它僅使用函數的調用來確定要為該函數呈現的DOM元素。

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.

最重要的是,ReactDOM不會在瀏覽器中呈現組件,也不會呈現元素(就保持術語element代表React.createElement的結果React.createElement )。 它還不渲染實例。 它呈現DOM元素。

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:

每個React App都以使用React元素render調用開始。 讓我們使用來自reactjs.org的HelloMessage示例,將其稍作修改以使其具有功能組件:

const Today = () => (  <div>Today is {new Date().toDateString()}</div>);
class HelloMessage extends React.Component {  render() {    return (      <React.Fragment>        <div>Hello {this.props.name}</div>        <Today />      </React.Fragment>    );  }}
ReactDOM.render(  <HelloMessage name="Taylor" />,  mountNode);

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

第一個React元素是我們在ReactDOM.render調用中開始的ReactDOM.render

<HelloMessage name="Taylor" /> // 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元素描述了要呈現的DOM樹應以HelloMessage組件和等于Taylorname prop值開頭。

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

React現在需要回答以下問題:什么是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.

每當React元素描述一個React組件時(就像我們上面的React元素一樣),React使用該組件將描述替換為組件返回的內容。 此時,它將為基于類的組件創建一個實例,并將該實例的引用保留在內存中。 它不會為基于功能的組件創建任何內容。 它只是調用它們。

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

HelloMessage組件返回的是一個描述React.Fragment組件的React元素。

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

React現在需要回答這個問題:什么是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.

在只有有效的DOM節點之前,React會繼續減少這些組件的未知描述。 React.Fragment的描述被翻譯成2個React元素,一個描述div ,另一個描述Today組件。

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

React現在需要回答這樣的問題:什么是Today

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

它調用Today函數來找出最后一個問題。 Today函數返回一個描述div的React元素。

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.

至此,虛擬樹包含了所有描述DOM節點的React元素。 React使用其對帳算法來找出要在瀏覽器中更新的內容。 用組件實例轉換的節點保留修改該實例的功能。

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:

學習React還是Node? 結帳我的書:

  • Learn React.js by Building Games

    通過構建游戲學習React.js

  • Node.js Beyond the Basics

    超越基礎的Node.js

翻譯自: https://www.freecodecamp.org/news/react-interview-question-what-gets-rendered-in-the-browser-a-component-or-an-element-1b3eac777c85/

react控制組件中元素

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395136.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395136.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395136.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java gc時自動收dump_Full?GC分析:設置Java?VM參數實現在Full?GC前后自動生成Dump

本文講解了如何設置JavaVM參數實現在FullGC前后自動生成Dump。共有三個VM參數需要設置&#xff1a;HeapDumpBeforeFullGC 實現在Full GC前dump。HeapDumpBeforeFullGC 實現在Full GC后dump。HeapDumpPath 設置Dump保存的路徑設置這些參數的方法&#xff0c;這里總結了四種&…

jquery插件dataTables自增序號。

dataTables官網提供了一種方式&#xff0c;使用后沒有達到預期效果&#xff08;js報錯&#xff09;&#xff0c;沒有深究原因。如果需要&#xff0c;可以按照下面的方式來。 1 $(#dataList).dataTable({2 "language": {3 "sProcessing&…

Maven使用詳解

1、maven介紹&#xff1a; 2、pom.xml文件理解&#xff1a; <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schema…

諾基亞報告稱:到2020年北美電子郵件流量占比將跌至7%

日前&#xff0c;諾基亞貝爾實驗室下屬貝爾實驗室咨詢部門&#xff08;Bell Labs Consulting&#xff09;發布研究報告稱&#xff0c;在北美&#xff0c;千禧一代青少年和青壯年消費群體正逐漸壯大&#xff0c;受其驅動的視頻通信流量占比將由47%增至86%。隨著視頻通話和視頻會…

開源貢獻 計算_我的第一個Hacktoberfest-第一次為開源做貢獻的經驗

開源貢獻 計算by Sibylle Sehl通過Sibylle Sehl 我的第一個Hacktoberfest-第一次為開源做貢獻的經驗 (My First Hacktoberfest — Experiences of Contributing to Open Source as a First Timer) Contributing to Open Source and projects can seem like a daunting process…

java web junit_如何使用junit測試javaweb工程

一:創建一個測試類,建議將測試類單獨放在一個包中(在 maven 項目里有測試類專門的存放位置),新建一個Junit Test Case類,下一步 測試類的命名建議是你將要測試的類名Test,然后點 Browse, 你可以選擇要進行測試的類(一般選擇 Service, 因為 Service 關心的是業務需求),用這種方式…

文件系統及程序的限制關系: ulimit

想像一個狀況&#xff1a;我的 Linux 主機里面同時登陸了十個人&#xff0c;這十個人不知怎么搞的&#xff0c; 同時打開了 100 個文件&#xff0c;每個文件的大小約 10MBytes &#xff0c;請問一下&#xff0c; 我的 Linux 主機的內存要有多大才夠&#xff1f; 1010010 10000…

java代碼_Java 代碼實現排序算法

閱讀本文約需要8分鐘 大家好&#xff0c;我是你們的導師&#xff0c;我每天都會在這里給大家分享一些干貨內容(當然了&#xff0c;周末也要允許老師休息一下哈)。上次老師跟大家分享了下SpringBootGradle MyBatisPlus3.x搭建企業級的后臺分離框架的相關知識&#xff0c;今天跟大…

移動游戲市場Testin云測占有率超過90%

《王者榮耀》、全民K歌、美團大眾、共享單車……越來越多的爆款應用占據著我們的手機桌面&#xff0c;也驅動著創業者不斷發掘新的移動應用和商業模式&#xff0c;卻鮮有人留意到&#xff0c;由移動應用催生出來的APP測試市場。 “現在用戶獲取成本是幾年前的幾十倍&#xff0c…

java 拆箱_Java自動裝箱拆箱

一、裝箱、拆箱定義如果一個int型量被傳遞到需要一個Integer對象的地方&#xff0c;那么&#xff0c;編譯器將在幕后插入一個對Integer構造方法的調用&#xff0c;這就叫做自動裝箱。而如果一個Integer對象被放到需要int型量的地方&#xff0c;則編譯器將幕后插入一個隊intValu…

我們如何使用CircleCI 2.0來構建Angular應用并將其部署到AWS S3

by Marius Lazar通過馬里烏斯拉扎爾(Marius Lazar) 我們如何使用CircleCI 2.0來構建Angular應用并將其部署到AWS S3 (How we used CircleCI 2.0 to build and deploy an Angular app to AWS S3) In today’s world, continuous integration and deployment (CI & CD) is a…

攜手助力新型智慧城市建設和科技創新發展

2017年5月9日&#xff0c;三門峽市政府與北京航天控制儀器研究所、溢思得瑞科技創新集團戰略合作協議簽約儀式舉行&#xff0c;共同推動三門峽市新型智慧城市建設和科技創新發展。 市委書記劉南昌&#xff0c;市委常委、宣傳部部長呂挺琳&#xff0c;副市長李琳&#xff0c;市城…

在采用vue-cli Post Get

需要依賴插件 vue-resource npm install vue-resource --save https://cn.vuejs.org/v2/cookbook/using-axios-to-consume-apis.html 采用axios一樣可以取數值 new Vue({ el: #app, data () { return { info: null } }, mounted () { axios .get(https://api.coindesk.com/v1/b…

優秀的開源項目C_適合提高C/C++、網絡編程能力的開源項目!不要錯過,趕緊收藏...

我們學習每一個編程語言都是有一個項目實戰的過程&#xff0c;而對于開發類的編程語言&#xff0c;除了適當的做項目程序外&#xff0c;學習了解其他的開源項目更是一個關鍵&#xff0c;就比如我們的C/C編程語言的學習。前陣子有一個小伙伴就問到我&#xff0c;我學好C/C基礎后…

Opencv分水嶺算法——watershed自動圖像分割用法

分水嶺算法是一種圖像區域分割法&#xff0c;在分割的過程中&#xff0c;它會把跟臨近像素間的相似性作為重要的參考依據&#xff0c;從而將在空間位置上相近并且灰度值相近的像素點互相連接起來構成一個封閉的輪廓&#xff0c;封閉性是分水嶺算法的一個重要特征。 其他圖像分割…

單變量線性回歸模型_了解如何為單變量模型選擇效果最好的線性回歸

單變量線性回歸模型by Bjrn Hartmann比約恩哈特曼(BjrnHartmann) 找出哪種線性回歸模型最適合您的數據 (Find out which linear regression model is the best fit for your data) Inspired by a question after my previous article, I want to tackle an issue that often c…

java javax.xml.ws_如何通過javax.xml.ws.Service進行調用

在Eclipse中創建了一個新的標準java 7項目,并成功設法獲取javax.xml.ws.Service的實例,如下所示&#xff1a;String wsdlURL "http://example.com:3000/v1_0/foo/bar/SomeService?wsdl";String namespace "http://foo.bar.com/webservice";String servi…

漢能:讓人類像葉綠素一樣利用太陽能

6月初&#xff0c;一批在車筐里同時標識了摩拜“Mobike”和漢能“Hanergy”的摩拜單車在北京投入使用。這是由漢能與摩拜合作開發的第一批裝有漢能薄膜太陽能組件的共享單車。 這批共享單車所裝載的5.5瓦的漢能MiaSol的柔性薄膜太陽能組件&#xff0c;將為摩拜車載智能鎖中內置…

Java Annotation

一、了解注釋注釋是java1.5 jdk這后引入的特性。Java庫自己帶的注釋有Deprecated, Overwrite等。注釋是加在類&#xff0c;方法&#xff0c;變量等上的一種標記。并且&#xff0c;可以通過javaj反射操作把這個標記取出來。主要用途是用于對方法&#xff0c;變量&#xff0c;類等…

pycharm顯示全部數據_PyCharm第一次安裝及使用教程

pycharm簡介PyCharm是一種Python IDE&#xff0c;帶有一整套可以幫助用戶在使用Python語言開發時提高其效率的工具&#xff0c;比如調試、語法高亮、Project管理、代碼跳轉、智能提示、自動完成、單元測試、版本控制。此外&#xff0c;該IDE提供了一些高級功能&#xff0c;以用…