Skip to content
This repository was archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
fix: don't use deprecated getNode() if not needed
Browse files Browse the repository at this point in the history
satya164 committed Aug 4, 2020
1 parent e0fd7e5 commit 73b9f63
Showing 2 changed files with 23 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/ScrollPager.tsx
Original file line number Diff line number Diff line change
@@ -85,7 +85,15 @@ export default class ScrollPager<T extends Route> extends React.Component<

private scrollTo = (x: number, animated = true) => {
if (this.scrollViewRef.current) {
this.scrollViewRef.current.getNode().scrollTo({
// getNode() is not necessary in newer versions of React Native
const scrollView =
// @ts-ignore
typeof this.scrollViewRef.current?.scrollTo === 'function'
? this.scrollViewRef.current
: this.scrollViewRef.current?.getNode();

// @ts-ignore
scrollView?.scrollTo({
x,
animated: animated,
});
25 changes: 14 additions & 11 deletions src/TabBar.tsx
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ import * as React from 'react';
import {
StyleSheet,
View,
ScrollView,
StyleProp,
ViewStyle,
TextStyle,
@@ -139,7 +138,7 @@ export default class TabBar<T extends Route> extends React.Component<

private scrollAmount = new Animated.Value(0);

private scrollView: ScrollView | undefined;
private scrollViewRef = React.createRef<Animated.ScrollView>();

private cancelNextFrameCb: (() => void) | undefined = undefined;

@@ -270,11 +269,18 @@ export default class TabBar<T extends Route> extends React.Component<

private resetScroll = (index: number) => {
if (this.props.scrollEnabled) {
this.scrollView &&
this.scrollView.scrollTo({
x: this.getScrollAmount(this.props, this.state, index),
animated: true,
});
// getNode() is not necessary in newer versions of React Native
const scrollView =
// @ts-ignore
typeof this.scrollViewRef.current?.scrollTo === 'function'
? this.scrollViewRef.current
: this.scrollViewRef.current?.getNode();

// @ts-ignore
scrollView?.scrollTo({
x: this.getScrollAmount(this.props, this.state, index),
animated: true,
});
}
};

@@ -413,10 +419,7 @@ export default class TabBar<T extends Route> extends React.Component<
},
},
])}
ref={(el) => {
// @ts-ignore
this.scrollView = el?.getNode();
}}
ref={this.scrollViewRef}
>
{routes.map((route: T) => {
const props: TabBarItemProps<T> & { key: string } = {

0 comments on commit 73b9f63

Please sign in to comment.