Skip to content

Instantly share code, notes, and snippets.

@rgommezz
Last active April 2, 2019 05:57
Show Gist options
  • Save rgommezz/1c914f179b9f86e8eea6592186253466 to your computer and use it in GitHub Desktop.
Save rgommezz/1c914f179b9f86e8eea6592186253466 to your computer and use it in GitHub Desktop.
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