简体   繁体   English

将React组件与HTML块混合

[英]Mixing React Components with HTML Blocks

Something that's always confused me is how many React examples show everything as components. 一直让我感到困惑的是,有多少个React示例将所有内容显示为组件。 But's let's say I only need to include some simple HTML alongside my components, like: 但让我们说,我只需要在组件旁边包含一些简单的HTML,例如:

class Homepage extends Component {
  render() {
    return (
      <Container>
        <Hero />
        <Features />
        <div className="copyright">Copyright Some Company Name.</div>
      </Container>
    );
  }
}

The div element will never be more than static text. div元素永远不会超过静态文本。

Should the <div> be moved into a pure component? 应该将<div>移到纯组件中吗? Or is it ok to simplify the Homepage component by adding plain HTML this? 还是可以通过添加纯HTML来简化Homepage组件?

Sure it's ok. 当然可以。 all in all it's HTML in the end. 总而言之,最后是HTML。 React components are set of html elements when you call the render function. 当您调用render函数时,React组件是一组html元素。

One rule of thumb i follow is: create a new component when you think a new responsibility is in order. 我遵循的一条经验法则是:当您认为有新职责时,请创建一个新组件。

That div is a component just like any other, except it is a "primitive" one. div与其他任何div一样都是一个组件,只是它是“原始”组件。 There should be no problem mixing the primitive components from HTML with your own custom components. 将HTML中的原始组件与您自己的自定义组件混合应该没有问题。

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

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