Skip to content

Commit

Permalink
[Performance] Fix FlatList memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
brunolemos committed Jul 12, 2019
1 parent d9d649b commit 8abc84b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
16 changes: 10 additions & 6 deletions packages/components/src/components/cards/EventCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ function keyExtractor(item: EnhancedGitHubEvent, _index: number) {
return `event-card-${item.id}`
}

const stickyHeaderIndices = [0]

export const EventCards = React.memo((props: EventCardsProps) => {
const {
cardViewMode,
Expand Down Expand Up @@ -180,9 +182,9 @@ export const EventCards = React.memo((props: EventCardsProps) => {
enableCompactLabels={enableCompactLabels}
event={item}
isFocused={
!disableItemFocus &&
column.id === focusedColumnId &&
item.id === selectedItemIdRef.current &&
!disableItemFocus
item.id === selectedItemIdRef.current
}
repoIsKnown={props.repoIsKnown}
swipeable={props.swipeable}
Expand All @@ -193,7 +195,9 @@ export const EventCards = React.memo((props: EventCardsProps) => {

const renderItem = useCallback(_renderItem, [
cardViewMode,
column.id === focusedColumnId && selectedItemIdRef.current,
!disableItemFocus &&
column.id === focusedColumnId &&
selectedItemIdRef.current,
enableCompactLabels,
props.swipeable,
props.repoIsKnown,
Expand Down Expand Up @@ -348,7 +352,7 @@ export const EventCards = React.memo((props: EventCardsProps) => {
)
}

function renderEmptyComponent() {
const renderEmptyComponent = useCallback(() => {
return (
<EmptyCards
column={column}
Expand All @@ -359,7 +363,7 @@ export const EventCards = React.memo((props: EventCardsProps) => {
refresh={refresh}
/>
)
}
}, [column, errorMessage, fetchNextPage, loadState, refresh])

return (
<FlatList
Expand All @@ -384,7 +388,7 @@ export const EventCards = React.memo((props: EventCardsProps) => {
refreshControl={refreshControl}
removeClippedSubviews
renderItem={renderItem}
stickyHeaderIndices={[0]}
stickyHeaderIndices={stickyHeaderIndices}
viewabilityConfig={viewabilityConfig}
windowSize={2}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function keyExtractor(item: EnhancedGitHubIssueOrPullRequest) {
return `issue-or-pr-card-${item.id}`
}

const stickyHeaderIndices = [0]

export const IssueOrPullRequestCards = React.memo(
(props: IssueOrPullRequestCardsProps) => {
const {
Expand Down Expand Up @@ -196,9 +198,9 @@ export const IssueOrPullRequestCards = React.memo(
cardViewMode={cardViewMode}
enableCompactLabels={enableCompactLabels}
isFocused={
!disableItemFocus &&
column.id === focusedColumnId &&
item.id === selectedItemIdRef.current &&
!disableItemFocus
item.id === selectedItemIdRef.current
}
issueOrPullRequest={item}
repoIsKnown={props.repoIsKnown}
Expand All @@ -210,7 +212,9 @@ export const IssueOrPullRequestCards = React.memo(
}
const renderItem = useCallback(_renderItem, [
cardViewMode,
column.id === focusedColumnId && selectedItemIdRef.current,
!disableItemFocus &&
column.id === focusedColumnId &&
selectedItemIdRef.current,
enableCompactLabels,
props.repoIsKnown,
props.swipeable,
Expand Down Expand Up @@ -350,7 +354,7 @@ export const IssueOrPullRequestCards = React.memo(
)
}

function renderEmptyComponent() {
const renderEmptyComponent = useCallback(() => {
const maybeInvalidFilters = `${errorMessage || ''}`
.toLowerCase()
.startsWith('validation failed')
Expand Down Expand Up @@ -410,7 +414,14 @@ export const IssueOrPullRequestCards = React.memo(
refresh={refresh}
/>
)
}
}, [
column,
errorMessage,
fetchNextPage,
loadState,
refresh,
loggedUsername,
])

return (
<FlatList
Expand All @@ -436,7 +447,7 @@ export const IssueOrPullRequestCards = React.memo(
refreshControl={refreshControl}
removeClippedSubviews
renderItem={renderItem}
stickyHeaderIndices={[0]}
stickyHeaderIndices={stickyHeaderIndices}
viewabilityConfig={viewabilityConfig}
windowSize={2}
/>
Expand Down
16 changes: 10 additions & 6 deletions packages/components/src/components/cards/NotificationCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function keyExtractor(item: EnhancedGitHubNotification) {
return `notification-card-${item.id}`
}

const stickyHeaderIndices = [0]

export const NotificationCards = React.memo((props: NotificationCardsProps) => {
const {
cardViewMode,
Expand Down Expand Up @@ -177,9 +179,9 @@ export const NotificationCards = React.memo((props: NotificationCardsProps) => {
cardViewMode={cardViewMode}
enableCompactLabels={enableCompactLabels}
isFocused={
!disableItemFocus &&
column.id === focusedColumnId &&
item.id === selectedItemIdRef.current &&
!disableItemFocus
item.id === selectedItemIdRef.current
}
notification={item}
repoIsKnown={props.repoIsKnown}
Expand All @@ -190,7 +192,9 @@ export const NotificationCards = React.memo((props: NotificationCardsProps) => {
}
const renderItem = useCallback(_renderItem, [
cardViewMode,
column.id === focusedColumnId && selectedItemIdRef.current,
!disableItemFocus &&
column.id === focusedColumnId &&
selectedItemIdRef.current,
enableCompactLabels,
props.repoIsKnown,
props.swipeable,
Expand Down Expand Up @@ -322,7 +326,7 @@ export const NotificationCards = React.memo((props: NotificationCardsProps) => {
)
}

function renderEmptyComponent() {
const renderEmptyComponent = useCallback(() => {
return (
<EmptyCards
clearMessage="No new notifications!"
Expand All @@ -334,7 +338,7 @@ export const NotificationCards = React.memo((props: NotificationCardsProps) => {
refresh={refresh}
/>
)
}
}, [column, errorMessage, fetchNextPage, loadState, refresh])

return (
<FlatList
Expand All @@ -360,7 +364,7 @@ export const NotificationCards = React.memo((props: NotificationCardsProps) => {
refreshControl={refreshControl}
removeClippedSubviews
renderItem={renderItem}
stickyHeaderIndices={[0]}
stickyHeaderIndices={stickyHeaderIndices}
viewabilityConfig={viewabilityConfig}
windowSize={2}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/libs/flatlist/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CrossPlatformFlatList, CrossPlatformFlatListProps } from './types'
export interface FlatListProps<ItemT>
extends CrossPlatformFlatListProps<ItemT> {}

export class FlatList<ItemT> extends React.Component<FlatListProps<ItemT>>
export class FlatList<ItemT> extends React.PureComponent<FlatListProps<ItemT>>
implements CrossPlatformFlatList<ItemT> {
ref = React.createRef<CrossPlatformFlatList<ItemT>>()

Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/libs/flatlist/index.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface FlatListProps<ItemT>
keyExtractor: (item: ItemT, index: number) => string
}

export class FlatList<ItemT> extends React.Component<FlatListProps<ItemT>>
export class FlatList<ItemT> extends React.PureComponent<FlatListProps<ItemT>>
implements CrossPlatformFlatList<ItemT> {
static displayName = 'FlatList (react-window)'

Expand Down

0 comments on commit 8abc84b

Please sign in to comment.