代码之家  ›  专栏  ›  技术社区  ›  Radex

如何渲染反应组件x次?

  •  1
  • Radex  · 技术社区  · 8 年前

    我有以下组件,它应该在p标记中重复hello x time作为repeat参数,我有一个问题,dom中没有渲染任何内容,render的返回为空。

    如何修复它?

     const content = 'hello';
    
     const render= (repeat?: number) => {
       const arr = new Array(repeat || 1)
       return arr.map(n => <p key={n}>{content}</p>)
     }
    
     export const Print = ({ classes, repeat }: Props) => (
       <div>{render(repeat)}</div>
     )
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Andy    8 年前

    您应该更新:

    const arr = new Array(repeat || 1);
    

    到

    const arr = new Array(repeat || 1).fill(null);
    

    否则你最终会 array of empty slots . 不必这样 null 顺便说一句,只要你面前的插槽里有东西 map 在元素上方。 地图 在空插槽上不工作。

        2
  •  0
  •   qpzm    8 年前

    你应该改变 renderContent 到 render 因为前者没有定义。