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

Fix: the tab header not sync with scene swipe when header is long #1402

Closed
wants to merge 3 commits into from
Closed
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
17 changes: 10 additions & 7 deletions src/TabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ export default function TabBar<T extends Route>({
const isFirst = React.useRef(true);
const scrollAmount = useAnimatedValue(0);
const measuredTabWidths = React.useRef<Record<string, number>>({});
const measureTabWidthsTimer = React.useRef<null | ReturnType<
typeof setTimeout
>>(null);

const { routes } = navigationState;
const flattenedTabWidth = getFlattenedTabWidth(tabStyle);
Expand All @@ -296,9 +299,10 @@ export default function TabBar<T extends Route>({
flattenedTabWidth,
});

const mesureRoutes = routes.slice(0, navigationState.index + 1);
const hasMeasuredTabWidths =
Boolean(layout.width) &&
routes.every((r) => typeof tabWidths[r.key] === 'number');
mesureRoutes.every((r) => typeof tabWidths[r.key] === 'number');

React.useEffect(() => {
if (isFirst.current) {
Expand Down Expand Up @@ -375,13 +379,12 @@ export default function TabBar<T extends Route>({

// When we have measured widths for all of the tabs, we should updates the state
// We avoid doing separate setState for each layout since it triggers multiple renders and slows down app
if (
routes.every(
(r) => typeof measuredTabWidths.current[r.key] === 'number'
)
) {
setTabWidths({ ...measuredTabWidths.current });
if (measureTabWidthsTimer.current) {
clearTimeout(measureTabWidthsTimer.current);
}
measureTabWidthsTimer.current = setTimeout(() => {
setTabWidths({ ...measuredTabWidths.current });
}, 300);
}
: undefined,
onPress: () => {
Expand Down