MCPcopy Create free account
hub / github.com/effector/effector / useListBase

Function useListBase

src/react/apiBase.ts:258–347  ·  view source on GitHub ↗
(
  list: Store<T[]>,
  renderItem:
    | {
        keys?: any[]
        fn(item: T, index: number): React.ReactNode
        getKey?: (item: T) => string
        placeholder?: React.ReactNode
      }
    | ((item: T, index: number) => React.ReactNode),
  scope?: Scope,
)

Source from the content-addressed store, hash-verified

256 return value
257}
258export function useListBase<T>(
259 list: Store<T[]>,
260 renderItem:
261 | {
262 keys?: any[]
263 fn(item: T, index: number): React.ReactNode
264 getKey?: (item: T) => string
265 placeholder?: React.ReactNode
266 }
267 | ((item: T, index: number) => React.ReactNode),
268 scope?: Scope,
269): React.ReactNode {
270 let keys = [] as any[]
271 let fn
272 let getKey: ((item: T) => string) | void
273 let placeholder: React.ReactNode | void
274 if (typeof renderItem === 'object' && renderItem !== null) {
275 if (renderItem.keys) keys = renderItem.keys
276 ;({fn, getKey, placeholder} = renderItem)
277 } else {
278 fn = renderItem
279 }
280 if (!is.store(list)) throwError('expect useList first argument to be a store')
281 if (typeof fn !== 'function')
282 throwError("expect useList's renderItem to be a function")
283 if (!Array.isArray(keys)) throwError("expect useList's keys to be an array")
284 const Item = React.useMemo(() => {
285 const Item = withDisplayName(
286 `${list.shortName || 'Unknown'}.Item`,
287 (
288 props:
289 | {index: number; keys: any[]; keyVal: never; value: never}
290 | {index: never; keys: any[]; keyVal: string; value: T},
291 ) => {
292 const {index, keys, keyVal, value} = props
293 const isKeyed = !!fnRef.current[1]
294 if (isKeyed) {
295 return fnRef.current[0](value, keyVal as any)
296 }
297 const item = useStoreMapBase(
298 [
299 {
300 store: list,
301 keys: [index, ...keys],
302 fn: (list, keys) => list[keys[0]],
303 },
304 ],
305 scope,
306 )
307 return fnRef.current[0](item, index)
308 },
309 )
310 return React.memo(Item)
311 }, [list, scope, !!getKey!])
312 const fnRef = React.useRef([fn, getKey!] as const)
313 fnRef.current = [fn, getKey!]
314 const keysSelfMemo = React.useMemo(() => keys, keys)
315 if (getKey!) {

Callers 2

useListFunction · 0.90
useListFunction · 0.90

Calls 6

throwErrorFunction · 0.90
withDisplayNameFunction · 0.90
storeMethod · 0.80
useStoreMapBaseFunction · 0.70
useUnitBaseFunction · 0.70
mapMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…