React component closing tag(React 组件结束标签)
问题描述
我是 React 新手,我正在尝试弄清楚 <MyComponent></MyComponent> 的用途/用途.与 <我的组件/>.除了自动关闭标签,我似乎找不到任何信息.
I'm new to React and I'm trying to figure out the purpose/use of <MyComponent></MyComponent> vs <MyComponent />. I can't seem to find information on anything except self-closing tags.
我使用自动关闭的 <MyComponent 创建了一个基本的标签滚动条作为 JSFiddle/>和后续的道具,我想知道是否有比我所做的更好的 React 编写方法.
I've created a basic tab scroller as a JSFiddle using the self-closing <MyComponent /> and subsequent props, and I'm wondering if there's a better way to write in React than what I've done.
class TabScroller extends React.Component {
render() {
return (
<div className="tabScroller">
<div className="NavList">
<TabNav handleClick={this.handleNavClick} />
<TabList
tabs={this.state.tabs}
activeTab={this.state.activeTab}
scrollPosition={this.state.scrollPosition}
handleClick={this.handleTabClick}
/>
</div>
<TabContent content={this.state.tabs[this.state.activeTab].content} />
</div>
);
}
}
// ========================================
ReactDOM.render(
<TabScroller />,
document.getElementById('root')
);
推荐答案
在 React 的 JSX 中,只有当组件有子组件时,才需要编写 <MyComponent></MyComponent>
,比如这个:
In React's JSX, you only need to write <MyComponent></MyComponent>
when the component has child components, like this:
<MyComponent>
<Child />
<Child />
<Child />
</MyComponent>
如果<MyComponent>
和</MyComponent>
之间没有任何内容,那么你可以写成<MyComponent/>
或 <MyComponent></MyComponent>
(但通常首选
If there is nothing between <MyComponent>
and </MyComponent>
, then you can write it either <MyComponent/>
or <MyComponent></MyComponent>
(but <MyComponent/>
is generally preferred). Details in Introducing JSX.
顺便说一句,您可以通过特殊的 props.children
属性访问组件中的这些子项.深入了解 JSX:JSX 中的子项 中的更多内容.
Just as a side note, you'd access those children in your component via the special props.children
property. More in JSX in Depth: Children in JSX.
请注意,这与 HTML 或 XHTML 非常不.这是具有不同规则的自己的(相似的)事物.例如,在 HTML 中,<div/>
与 <div>
完全相同:一个 start 标签,其中你最终必须有一个结束标签.不是 JSX(或 XHTML).HTML 的规则是 void 元素(从不具有标记内容的元素,例如 br
或 img
)可以用或不用 /
在 >
之前,它们永远不会得到结束标记,但非空元素(如 div
)必须始终有一个结束标记(</div>
),它们不能自动关闭.在 JSX(和 XHTML)中,它们可以.
Note that this is very much not like HTML or XHTML. It's its own (similar) thing with different rules. For instance, in HTML, <div/>
is exactly the same thing as <div>
: A start tag, for which you must eventually have an end tag. Not so JSX (or XHTML). The rules for HTML are that void elements (elements that never have markup content, such as br
or img
) can be written with or without /
before >
and they never get an ending tag, but non-void elements (like div
) must always have an ending tag (</div>
), they cannot be self-closing. In JSX (and XHTML), they can be.
这篇关于React 组件结束标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:React 组件结束标签


基础教程推荐
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01