简体   繁体   English

在javascript中干燥html包装器

[英]DRYing html wrappers in javascript

I am using React js and am building a lot of repeating elements that are all getting wrapped in the same html, like so: 我正在使用React js并正在构建很多重复元素,这些元素都被包装在同一个html中,如下所示:

<div>
  <div class="child">
    <div class="grand-child">
      <Element1 />
    </div>
  </div>
  <div class="child">
    <div class="grand-child">
      <Element2 />
    </div>
  </div>
  <div class="child">
    <div class="grand-child">
      <Element3 />
    </div>
  </div>
</div>

Instead of having to constantly wrap every Element in 'child' and 'grand-child' divs, is there a simpler way I can write this so I don't have to repeat myself? 不必将每个Element始终包裹在“ child”和“ grand-child” div中,有没有一种更简单的方法可以编写此代码,因此我不必重复自己?

I've looked into something like the innerHTML property, which tags an html element and inserts information / elements inside of that original element. 我已经研究了诸如innerHTML属性之类的属性,该属性标记html元素并将信息/元素插入该原始元素内。 What I would like to do is the opposite and instead take my original element and wrap it with other html elements, but it seems like the outerHTML property does not operate in this manner. 我想做的是相反的,取而代之的是使用我的原始元素并将其与其他html元素包装在一起,但是似乎outerHTML属性无法以这种方式运行。

Is there any way to wrap html elements as seen in this psuedo solution below? 有什么方法可以包装html元素,如下面的此伪解决方案所示? Thanks. 谢谢。

Let <foo></foo> =
  <div class="child">
    <div class="grand-child">
    </div>
  </div>

<div class="parent">
  <foo>
    <p>This is the first element</p>
  </foo>
  <foo>
    <p>This is the second element</p>
  </foo>
  <foo>
    <p>This is the third element</p>
  </foo>
</div>

There are a few good ways to do this: (1) using an array to store the contents and .map ping to the markup you want, and (2) creating a separate component to be the wrapper, passing along the contents as children . 有几种不错的方法可以做到这一点:(1)使用数组存储内容并将.map ping .map到所需的标记,以及(2)创建一个单独的组件作为包装,将内容作为children元素传递。 You could even combine these, depending on how reusable you want the wrapper component to be: 您甚至可以结合使用这些组件,具体取决于包装器组件的可重用性:

(1) This should produce the same markup you have in your first example, using .map : (1)这应该使用.map产生与第一个示例相同的标记:

<div>
  {[Element1, Element2, Element3].map((Element, index) => (
    <div class="child" key={index}>
      <div class="grand-child">
        <Element />
      </div>
    </div>
  ))}
</div>

(2) If you want to break out to a separate component, you could do the following, using a stateless functional "Wrapper" component with props.children accessing the passed-down content: (2)如果要分解为单独的组件,则可以使用无状态功能性的“包装器”组件,并通过props.children访问传递的内容来执行以下操作:

const Wrapper = props => (
  <div class="child">
    <div class="grand-child">
      {props.children}
    </div>
  </div>
)

...

<div>
  <Wrapper>
    <Element1 />
  </Wrapper>
  <Wrapper>
    <Element2 />
  </Wrapper>
  <Wrapper>
    <Element3 />
  </Wrapper>
</div>

Finally, if you wanted to combine these, you can create a wrapper component and use it within a .map call to pass it different contents: 最后,如果您想组合使用这些组件,则可以创建一个包装器组件,并在.map调用中使用它来传递不同的内容:

const Wrapper = props => (
  <div class="child">
    <div class="grand-child">
      {props.children}
    </div>
  </div>
)

...


<div>
  {[Element1, Element2, Element3].map((Element, index) => (
    <Wrapper key={index}>
      <Element />
    </Wrapper>
  ))}
</div>

Hi you need children . 嗨,你需要children So for you case I would suggest create wrapper Component. 因此,对于您而言,我建议创建包装器组件。

 const ChildrenWrapper = ({children}) => { return ( <div className="child"> <div className="grand-child"> {children} </div> </div> ); } 

And use it 并使用它

 <div class="parent"> <ChildWrapper> <p>This is the first element</p> </ChildWrapper> <ChildWrapper> <p>This is the second element</p> </ChildWrapper> <ChildWrapper> <p>This is the third element</p> </ChildWrapper> </div> 

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

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