简体   繁体   English

使用React Component重复HTML部分

[英]Repeat HTML parts with React Component

I'my trying to duplicate a React Component with another component. 我正在尝试与另一个组件复制一个React组件。

My component to repeat a section 我的组件重复一节

class RepeatHtml extends React.Component {
render() {
    var names = new Array(parseInt(this.props.number)).fill().map((_, i) => i);
    var namesList = names.map(function (name, index) {
        return (
            <li>{index + 1}</li>);
    });
    return <ul>{ namesList }</ul>
    }
}

And I want to repeat another component of my choice with that, like 我想重复我选择的另一部分,例如

<RepeatHtml number="5" child="<InputElement />" />

Is it possible how ? 有可能吗?

<Repeat num={ num } component={ InputElement } />

const Repeat = ({ num, component as Component }) => (
  <ul>
    {
      ...
      names.map(name => (
        <li key={ uniqueKey }>
          <Component />
        </li>
      )
    }
  </ul>
)

You pass the component as props, dont use JSX. 您将组件作为道具传递,请勿使用JSX。 and then inside your map, render it as JSX. 然后在您的地图内部,将其渲染为JSX。

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

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