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

fix: do not use animated scroll on initial render #1009

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions src/TabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type Props<T extends Route> = SceneRendererProps & {

type State = {
layout: Layout;
tabWidths: { [key: string]: number };
tabWidths?: { [key: string]: number };
};

export default class TabBar<T extends Route> extends React.Component<
Expand All @@ -87,7 +87,7 @@ export default class TabBar<T extends Route> extends React.Component<

state: State = {
layout: { width: 0, height: 0 },
tabWidths: {},
tabWidths: undefined,
};

componentDidUpdate(prevProps: Props<T>, prevState: State) {
Expand All @@ -101,20 +101,18 @@ export default class TabBar<T extends Route> extends React.Component<
prevState.layout.width !== layout.width ||
prevState.tabWidths !== tabWidths
) {
if (
this.getFlattenedTabWidth(this.props.tabStyle) === 'auto' &&
!(
layout.width &&
navigationState.routes.every(
(r) => typeof tabWidths[r.key] === 'number'
)
)
) {
// When tab width is dynamic, only adjust the scroll once we have all tab widths and layout
const isWidthDynamic =
this.getFlattenedTabWidth(this.props.tabStyle) === 'auto';
if (isWidthDynamic && (!layout.width || !tabWidths)) {
return;
}

this.resetScroll(navigationState.index);
// Do not use animated scroll on initial render
// (that is, when layout is not yet calculated)
const animatedScroll = isWidthDynamic
? !!prevState.tabWidths && !!prevState.layout.width
: !!prevState.layout.width;
this.scrollToTab(navigationState.index, animatedScroll);
}
}

Expand All @@ -137,11 +135,11 @@ export default class TabBar<T extends Route> extends React.Component<
layout: Layout,
routes: Route[],
scrollEnabled: boolean | undefined,
tabWidths: { [key: string]: number },
tabWidths: { [key: string]: number } | undefined,
flattenedWidth: string | number | undefined
) => {
if (flattenedWidth === 'auto') {
return tabWidths[routes[index].key] || 0;
return tabWidths?.[routes[index].key] || 0;
}

switch (typeof flattenedWidth) {
Expand All @@ -168,7 +166,7 @@ export default class TabBar<T extends Route> extends React.Component<
layout: Layout,
routes: Route[],
scrollEnabled: boolean | undefined,
tabWidths: { [key: string]: number },
tabWidths: { [key: string]: number } | undefined,
flattenedWidth: string | number | undefined
) => (i: number) =>
this.getComputedTabWidth(
Expand Down Expand Up @@ -251,12 +249,12 @@ export default class TabBar<T extends Route> extends React.Component<
return this.normalizeScrollValue(props, state, scrollAmount);
};

private resetScroll = (index: number) => {
private scrollToTab = (index: number, animated: boolean = true) => {
if (this.props.scrollEnabled) {
this.scrollView &&
this.scrollView.scrollTo({
x: this.getScrollAmount(this.props, this.state, index),
animated: true,
animated,
});
}
};
Expand Down