简体   繁体   English

如何在反应中的 div class='App' 之前添加多个 static html 元素?

[英]How can I add multiple static html elements before the div class='App' in react?

I need to add 3 empty divs before the of my reactjs web page for special styling reasons, but I get an error if I do that:出于特殊样式原因,我需要在我的 reactjs web 页面之前添加 3 个空 div,但如果这样做,我会收到错误消息:

Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag.解析错误:相邻的 JSX 元素必须包含在封闭标记中。 Did you want a JSX fragment <>...</>?您想要一个 JSX 片段 <>...</> 吗?

What I need is我需要的是

<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div class='App'> 
...
</div>

You are getting this error because you are trying to return multiple JSX elements which is not how React works您收到此错误是因为您试图返回多个 JSX 元素,这不是 React 的工作方式

You are trying to do this:您正在尝试这样做:

<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div class='App'> 
...
</div>

But react will expect that you return single div with all the elemets inside, so pretty much all you need to do is to wrap them all in one div like this:但是 React 会期望您返回包含所有元素的单个 div,因此您几乎需要做的就是将它们全部包装在一个 div 中,如下所示:

<div>
    <div id="one"></div>
    <div id="two"></div>
    <div id="three"></div>
    <div class='App'> 
    ...
    </div>
</div>

Your error message contains the details you need:您的错误消息包含您需要的详细信息:

Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag.解析错误:相邻的 JSX 元素必须包含在封闭标记中。 Did you want a JSX fragment <>...</>?您想要一个 JSX 片段 <>...</> 吗?

This means you need to wrap your elements in a JSX fragment , like so:这意味着您需要将元素包装在JSX 片段中,如下所示:

<>
  <div id="one"></div>
  <div id="two"></div>
  <div id="three"></div>
  <div class='App'> 
    ...
  </div>
</>

Note that <>...</> is simply shorthand for <React.Fragment>...</React.Fragment> .请注意<>...</>只是<React.Fragment>...</React.Fragment>的简写。

暂无
暂无

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

相关问题 如何使用 HTML/JavaScript 添加多个元素 - How can I add multiple elements with HTML/JavaScript 如何使用jQuery向多个元素添加条件类? - How can I add a conditional class to multiple elements using jQuery? 如何将mouseenter()或mouseleave()添加到同一类的多个元素? - How can I add mouseenter() or mouseleave() to multiple elements of the same class? 我的代码不起作用。 如何在剪切的 div 上添加元素之后和之前? - My code is not working. How can I add after and before elements on a clipped div? 我可以将 React 应用程序用作静态 HTML 页面上的组件吗 - Can I use a React app as a component on a static HTML page 在jQuery中,如果我有一个开始和结束类,我怎么能用div包装多个元素 - In jQuery how can I wrap multiple elements with a div given that I have a start and end class 我如何使用 jQuery 删除除特定 class 之外的所有 div 和其他 DOM HTML 元素 - How can i remove all div's and other DOM HTML elements except a particular class using jQuery 如何根据 div 的类为 div 中的元素设置样式? - How can I style elements in a div based on the div's class? 如何添加多个元素,例如<span>,</span> <p> <span>要么</span> <div> <span>内</span> <option> <span>的标签</span> <select> <span>在ReactJs中?</span> - How can I add multiple elements like <span>, <p> or <div> inside <option> tag of <select> in ReactJs? 如果该div不包含文本,而是包含浮动HTML元素,如何在div中添加行号以使其看起来像IDE编辑器? - How can I add line numbers to a div to make it looks like an IDE editor if that div not containing text, instead it contains floating HTML elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM