Skip to content

useDynamicList 的 getIndex 能否指定比较的参数? #2280

Open
@Lizhooh

Description

看代码是直接对比,太局限了,基本只能原子类型的数组能用
我的 list 是 { key: string, name: string }[],需要根据 key 找出 index,理解性用 getIndex(key),看源码发现不对劲

 const getIndex = useCallback(
    (key: number) => keyList.current.findIndex((ele) => ele === key),
    [],
  );

建议加个第二个参数

 const getIndex = useCallback(
    (key: number, attr?: string | (val: T) => boolean) => keyList.current.findIndex((ele) => {
         if (typeof attr === 'function') return attr(ele)
         if (typeof  attr === 'string') return ele[attr] === key
         return ele === key
      }),
    [],
  );

Metadata

Assignees

Labels

featureNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions