简体   繁体   English

我应该在React Project中的哪里放置html wrapper元素?

[英]Where should I put an html wrapper element in a React Project?

I am starting with react. 我从反应开始。 So I have this simple project with bootstrap 所以我有一个带有引导程序的简单项目

I want to reuse an HTML structure: 我想重用HTML结构:

<div className="container">
  <div className="row">
    <div className="col-lg-8 col-md-10 mx-auto">
      {props.children}
    </div>
  </div>
</div>

So, everytime that I call this structure 3 div's will wrap my child element. 因此,每次我将此结构称为3 div时,都会包装我的子元素。 right?. 对?。

At the beginning I create something like this in my " header " component 一开始,我在“ header ”组件中创建了类似的内容

const Wrapper = (props) => (
        <div className="container">
          <div className="row">
            <div className="col-lg-8 col-md-10 mx-auto">
                  {props.children}
            </div>
          </div>
        </div>
)

And was easy to call that element from the class: 而且很容易从类中调用该元素:

function HeaderContent (props) {
  return (
  <SimpleWrapper>
    <div className="col-lg-8 col-md-10 mx-auto">
      <div className="site-heading">
          <h1>{props.h1title}</h1>
        <span className="subheading">{props.spantitle}</span>
      </div>
    </div>
  </SimpleWrapper>
  )
}

But now, I want to recall the wrapper from other components. 但是现在,我想从其他组件中调用包装器。 What should I do? 我该怎么办? My solution was to create a separate component called: Wrapper.js and declare my wrapper inside it. 我的解决方案是创建一个单独的组件: Wrapper.js并在其中声明我的包装器。 Is this viable? 这可行吗?

thanks, and sorry for the dummy question. 谢谢,对不起这个虚假的问题。

反应的前提是将UI分解为独立且可重复使用的组件,因此您建议的添加“包装”组件的解决方案是正确的。

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

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