Javascript 如何将react组件渲染x次?
我有以下组件,它应该在一个p标记中作为repeat参数重复hello x time,我有一个问题,dom中没有渲染任何东西,render的返回为空 如何修复它Javascript 如何将react组件渲染x次?,javascript,reactjs,Javascript,Reactjs,我有以下组件,它应该在一个p标记中作为repeat参数重复hello x time,我有一个问题,dom中没有渲染任何东西,render的返回为空 如何修复它 const content = 'hello'; const render= (repeat?: number) => { const arr = new Array(repeat || 1) return arr.map(n => <p key={n}>{content}</p>)
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>
)
const content='hello';
常量渲染=(重复?:编号)=>{
const arr=新数组(重复| | 1)
返回arr.map(n=>{content}
)
}
导出常量打印=({classes,repeat}:Props)=>(
{渲染(重复)}
)
您应该将renderContent
更改为render
,因为前者未定义。您应该更新:
const arr = new Array(repeat || 1);
到
否则,你将以失败告终。它不必是null
btw,只要在元素上映射map
之前插槽中填充了一些内容即可<代码>映射在空插槽上不起作用
const arr = new Array(repeat || 1).fill(null);