使用 lodash times 方法的索引
index with lodash times method
我需要将组件复制“n”次。为此,我使用了 lodash
方法 times
。问题是我需要一个索引作为生成的组件的键,但它看起来没有。
我有以下代码:
export const MyComponent: React.FC<{times: number}> = ({ times }) => {
return (
<>
{_.times(times, () => (
//I need a key={index} in this div
<div className="bg-white border-4 border-white md:rounded-md md:p-2 content-center my-4 shadow w-full">
</div>
))}
</>
);
};
这将return里面的组件n次。
我试图做一个 return 组件的方法,并使用 useState
设置索引,但它进入无限循环。我想把一个大的随机数作为密钥,所以很难得到相同的,但我不喜欢那个解决方案。我想用这个方法,因为它很干净。
那么你认为我可以做些什么来给组件一个?
它作为函数参数传递给您:
_.times(times, (index) => (blabla))
我需要将组件复制“n”次。为此,我使用了 lodash
方法 times
。问题是我需要一个索引作为生成的组件的键,但它看起来没有。
我有以下代码:
export const MyComponent: React.FC<{times: number}> = ({ times }) => {
return (
<>
{_.times(times, () => (
//I need a key={index} in this div
<div className="bg-white border-4 border-white md:rounded-md md:p-2 content-center my-4 shadow w-full">
</div>
))}
</>
);
};
这将return里面的组件n次。
我试图做一个 return 组件的方法,并使用 useState
设置索引,但它进入无限循环。我想把一个大的随机数作为密钥,所以很难得到相同的,但我不喜欢那个解决方案。我想用这个方法,因为它很干净。
那么你认为我可以做些什么来给组件一个?
它作为函数参数传递给您:
_.times(times, (index) => (blabla))