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.mzph.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;以用…