Skip to content

Instantly share code, notes, and snippets.

@janicduplessis
Last active August 29, 2019 17:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janicduplessis/111c8ea467fe0ccf64ea269ac1761634 to your computer and use it in GitHub Desktop.
Save janicduplessis/111c8ea467fe0ccf64ea269ac1761634 to your computer and use it in GitHub Desktop.
// @flow
import React from 'react';
import hoistStatics from 'hoist-non-react-statics';
import { withNavigation } from 'react-navigation';
import type { Subscription } from '../../types';
export default function createNavigationAwareScrollable(Component: any) {
class ComponentWithStatusBar extends React.PureComponent<any> {
static displayName = `NavigationAwareScrollable(${Component.displayName ||
Component.name})`;
_subscription: ?Subscription;
_scrollRef = React.createRef();
componentDidMount() {
this._subscription = this.props.navigation.addListener('refocus', () => {
const scrollableNode = this.getNode();
if (this.props.navigation.isFocused() && scrollableNode !== null) {
if (scrollableNode.scrollToTop != null) {
scrollableNode.scrollToTop();
} else if (scrollableNode.scrollTo != null) {
scrollableNode.scrollTo({ y: 0 });
}
}
});
}
getNode() {
if (this._scrollRef.current === null) {
return null;
}
// Could be an animated component
return this._scrollRef.current.getNode != null
? this._scrollRef.current.getNode()
: this._scrollRef.current;
}
componentWillUnmount() {
if (this._subscription != null) {
this._subscription.remove();
}
}
render() {
return <Component ref={this._scrollRef} {...this.props} />;
}
}
return hoistStatics(withNavigation(ComponentWithStatusBar), Component);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment