Skip to content

Commit

Permalink
fix: memoize header background image on backgroundImage prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooks Becton committed Oct 24, 2020
1 parent bd96d8d commit 88182a4
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions src/StickyParallaxHeader.js
Expand Up @@ -29,6 +29,35 @@ import { getSafelyScrollNode, setRef } from './utils';
const { divide, Value, createAnimatedComponent, event, timing, ValueXY } = Animated;
const AnimatedScrollView = createAnimatedComponent(ScrollView);

const HeaderBackgroundImage = (props) => {
const { backgroundHeight, backgroundImage, background } = props;
const AnimatedImageBackground = createAnimatedComponent(ImageBackground);

return (
<AnimatedImageBackground
style={[
styles.headerStyle,
{
height: backgroundHeight,
},
]}
source={backgroundImage}>
{background}
</AnimatedImageBackground>
);
};

HeaderBackgroundImage.propTypes = {
background: node,
backgroundHeight: number,
backgroundImage: Image.propTypes.source,
};

const headerImagesAreEqual = (prevProps, props) =>
prevProps.backgroundImage.uri === props.backgroundImage.uri;

const MemoHeaderImageBackground = React.memo(HeaderBackgroundImage, headerImagesAreEqual);

class StickyParallaxHeader extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -240,25 +269,6 @@ class StickyParallaxHeader extends Component {
);
};

renderImageBackground = (backgroundHeight) => {
const { backgroundImage, background } = this.props;

const AnimatedImageBackground = createAnimatedComponent(ImageBackground);

return (
<AnimatedImageBackground
style={[
styles.headerStyle,
{
height: backgroundHeight,
},
]}
source={backgroundImage}>
{background}
</AnimatedImageBackground>
);
};

renderPlainBackground = (backgroundHeight) => {
const { background } = this.props;

Expand Down Expand Up @@ -405,9 +415,15 @@ class StickyParallaxHeader extends Component {
},
]}
/>
{backgroundImage
? this.renderImageBackground(scrollHeight)
: this.renderPlainBackground(scrollHeight)}
{backgroundImage ? (
<MemoHeaderImageBackground
backgroundHeight={scrollHeight}
backgroundImage={this.props.backgroundImage}
background={this.props.background}
/>
) : (
this.renderPlainBackground(scrollHeight)
)}
{this.renderForeground(scrollHeight)}
</View>
{shouldRenderTabs && this.renderTabs()}
Expand Down

0 comments on commit 88182a4

Please sign in to comment.