Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: memoize interpolated style to avoid extra work
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jan 5, 2020
1 parent d6d06d0 commit d8b88bd
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions packages/stack/src/views/Stack/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,38 @@ export default class Card extends React.Component<Props> {
}
};

// Memoize this to avoid extra work on re-render
private getInterpolatedStyle = memoize(
(
styleInterpolator: StackCardStyleInterpolator,
index: number,
current: Animated.AnimatedInterpolation,
next: Animated.AnimatedInterpolation | undefined,
layout: Layout,
insetTop: number,
insetRight: number,
insetBottom: number,
insetLeft: number
) =>
styleInterpolator({
index,
current: { progress: current },
next: next && { progress: next },
closing: this.isClosing,
swiping: this.isSwiping,
inverted: this.inverted,
layouts: {
screen: layout,
},
insets: {
top: insetTop,
right: insetRight,
bottom: insetBottom,
left: insetLeft,
},
})
);

// Keep track of the animation context when deps changes.
private getCardAnimationContext = memoize(
(
Expand Down Expand Up @@ -369,18 +401,17 @@ export default class Card extends React.Component<Props> {
...rest
} = this.props;

const interpolatedStyle = styleInterpolator({
const interpolatedStyle = this.getInterpolatedStyle(
styleInterpolator,
index,
current: { progress: current },
next: next && { progress: next },
closing: this.isClosing,
swiping: this.isSwiping,
inverted: this.inverted,
layouts: {
screen: layout,
},
insets,
});
current,
next,
layout,
insets.top,
insets.right,
insets.bottom,
insets.left
);

const animationContext = this.getCardAnimationContext(
index,
Expand Down

0 comments on commit d8b88bd

Please sign in to comment.