Skip to content

Commit

Permalink
Memoize components
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Sep 4, 2024
1 parent e584d34 commit 8f782af
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/components/Layout/AsyncList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PaginationResponse } from '@vocdoni/sdk'
import { PropsWithChildren } from 'react'
import { PropsWithChildren, useMemo } from 'react'
import { ContentError, NoResultsError } from '~components/Layout/ContentError'
import { LoadingCards, SkeletonCardsProps } from '~components/Layout/Loading'
import { Pagination } from '~components/Pagination/Pagination'
Expand Down Expand Up @@ -48,9 +48,14 @@ export const PaginatedAsyncList = <T,>({
return <LoadingCards spacing={4} {...skeletonProps} />
}

const memoizedComponents = useMemo(
() => elements?.map((element, index) => <Component key={index} element={element} index={index} />),
[elements]
)

return (
<ListDataDisplay elements={elements} {...rest}>
{elements?.map((element, index) => <Component key={index} element={element} index={index} />)}
{memoizedComponents}
{pagination && routedPagination && <RoutedPagination pagination={pagination} />}
{pagination && !routedPagination && <Pagination pagination={pagination} />}
</ListDataDisplay>
Expand Down

0 comments on commit 8f782af

Please sign in to comment.