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

Commit

Permalink
fix: handle both null and undefined in useScrollToTop
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Aug 29, 2019
1 parent cdbf1e9 commit c951027
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/native/src/useScrollToTop.tsx
Expand Up @@ -9,30 +9,31 @@ type ScrollableView =
| { scrollToOffset(options: ScrollOptions): void }
| { scrollResponderScrollTo(options: ScrollOptions): void };

type MaybeScrollableWrapperView =
| ScrollableView
| { getScrollResponder: () => ScrollableView }
| { getNode: () => ScrollableView };
type ScrollableWrapper =
| { getScrollResponder(): ScrollableView }
| { getNode(): ScrollableView }
| ScrollableView;

function getNodeFromRef(
ref: React.RefObject<MaybeScrollableWrapperView>
): ScrollableView | null {
if (ref.current === null) {
function getScrollableNode(ref: React.RefObject<ScrollableWrapper>) {
if (ref.current == null) {
return null;
}

// Support weird animated containers and Animated components.
if ('getScrollResponder' in ref.current) {
// If the view is a wrapper like FlatList, SectionList etc.
// We need to use `getScrollResponder` to get access to the scroll responder
return ref.current.getScrollResponder();
} else if ('getNode' in ref.current) {
// When a `ScrollView` is wraped in `Animated.createAnimatedComponent`
// we need to use `getNode` to get the ref to the actual scrollview
return ref.current.getNode();
} else {
return ref.current;
}
}

export default function useScrollToTop(
ref: React.RefObject<MaybeScrollableWrapperView>
ref: React.RefObject<ScrollableWrapper>
) {
const navigation = useNavigation();

Expand All @@ -47,7 +48,7 @@ export default function useScrollToTop(
// Run the operation in the next frame so we're sure all listeners have been run
// This is necessary to know if preventDefault() has been called
requestAnimationFrame(() => {
const scrollable = getNodeFromRef(ref);
const scrollable = getScrollableNode(ref);

if (isFocused && !e.defaultPrevented && scrollable) {
// When user taps on already focused tab, scroll to top
Expand Down

0 comments on commit c951027

Please sign in to comment.