简体   繁体   English

为什么 ReactJS 组件,它是一个 function,必须放在 ReactDom.render() 方法的内部标签中?

[英]Why does ReactJS component, which is a function, have to be put in inside tags at the ReactDom.render() method?

I am very very new to ReactJS and JavaScript in general, so I am having difficulty trying to make sense of the syntax.一般来说,我对 ReactJS 和 JavaScript 非常陌生,所以我很难理解语法。

I have the following very basic component:我有以下非常基本的组件:

import React from 'react'
import ReactDom from 'react-dom'

function Greetings()
{
  return <h1>Hello World</h1>
}

which I am trying to render using the below line:我正在尝试使用以下行进行渲染:

ReactDom.render(<Greetings/>, document.getElementById('root'))

Now I am having difficulty trying to make sense of the syntax:现在我很难理解语法:

ReactDom.render(<Greetings/>, document.getElementById('root')) , where Greetings() is a function. ReactDom.render(<Greetings/>, document.getElementById('root')) ,其中Greetings()是 function。

So what does enclosing it in tags mean?那么将它包含在标签中是什么意思呢?

The syntax is called JSX .语法称为JSX By using JSX, you are effectively calling React.createElement() .通过使用 JSX,您可以有效地调用React.createElement() So, if you don't want to use JSX ( <Component> ), you can replace it with React.createElement(Component) .因此,如果您不想使用 JSX ( <Component> ),可以将其替换为React.createElement(Component) In fact, this is what Babel does when you use React: replaces all JSX tags with calls to React.createElement .事实上,当你使用 React 时,这就是 Babel 所做的:将所有 JSX 标签替换为对React.createElement的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM