lodash-es#remove TypeScript Examples
The following examples show how to use
lodash-es#remove.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useCacheRender.ts From UUI with MIT License | 5 votes |
export function useArrayCacheRender<T>(
data: T[],
render: (data: T) => React.ReactNode,
options: {
id: (i: T) => string;
comparator?: (previous: T, current: T) => boolean;
},
) {
const [list, setList] = useState<{
id: string;
rendered: React.ReactNode;
}[]>([])
const previous = usePrevious<T[]>(data) as T[]
const current = data
useEffect(() => {
const isSameOne = (i: T, j: T) => options.id(i) === options.id(j)
const intersected = intersectionWith(previous, current, isSameOne)
const removing = xorWith(previous, intersected, isSameOne)
const adding = xorWith(current, intersected, isSameOne)
const updating = intersected.filter((i) => {
const p = previous.find((j) => options.id(i) === options.id(j))
const c = current.find((j) => options.id(i) === options.id(j))
if (!p) return false
if (!c) return false
return options.comparator ? options.comparator(c, p) : !isEqual(c, p)
})
const newList = clone(list)
for (const i of removing) {
remove(newList, (r) => r.id === options.id(i))
}
for (const i of updating) {
const index = list.findIndex((r) => r.id === options.id(i))
const c = current.find((c) => options.id(c) === options.id(i))
if (index > -1 && c) newList[index] = { id: options.id(c), rendered: render(c) }
}
for (const i of adding) {
newList.push({ id: options.id(i), rendered: render(i) })
}
setList(newList)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [previous, current])
const rendered = useMemo(() => {
return list.map((i) => i.rendered)
}, [list])
return rendered
}