【问题标题】:index with lodash times method使用 lodash 时间方法的索引
【发布时间】:2021-10-03 09:39:23
【问题描述】:

我需要复制一个组件“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>
       ))}
    </>
  );
};

这将返回 n 次内的组件。

我试图做一个返回组件的方法并使用useState 设置索引,但它进入了无限循环。我想把一个大的随机数作为密钥,所以很难得到相同的,但我不喜欢那个解决方案。我想用这个方法,因为它很干净。

那么你认为我能做些什么来给组件一个 a 呢?

【问题讨论】:

  • 使用简单/标准循环...为什么需要外部依赖?
  • @Marc 我喜欢它,因为它更干净。还是谢谢你!

标签: javascript node.js lodash


【解决方案1】:

它作为函数参数传递给你:

_.times(times, (index) => (blabla))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2019-05-17
    • 2017-12-06
    相关资源
    最近更新 更多