Last active
April 2, 2019 05:57
-
-
Save rgommezz/1c914f179b9f86e8eea6592186253466 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isClassComponent = Component => | |
Boolean(Component.prototype && Component.prototype.isReactComponent); | |
function getNewElementTree(children, instanceProps) { | |
const { scrollY, ...rest } = instanceProps; | |
return ( | |
<Animated.ScrollView | |
{...rest} | |
onScroll={Animated.event( | |
[{ nativeEvent: { contentOffset: { y: scrollY } } }], | |
{ | |
useNativeDriver: true, | |
}, | |
)} | |
scrollEventThrottle={1} | |
> | |
{children} | |
</Animated.ScrollView> | |
); | |
} | |
function withAnimatedScrollView(WrappedComponent) { | |
let renderTree; | |
if (isClassComponent(WrappedComponent)) { | |
return class Enhancer extends WrappedComponent { | |
render() { | |
renderTree = super.render(); | |
return getNewElementTree(renderTree.props.children, this.props); | |
} | |
}; | |
} | |
// If WrappedComponent is functional, we extend from React.Component instead | |
return class EnhancerFunctional extends React.Component { | |
render() { | |
// The below call is equivalent to super.render() in class based components | |
renderTree = WrappedComponent(this.props); | |
return getNewElementTree(renderTree.props.children, this.props); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment