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

Commit b0a8ad4

Browse files
authored
fix: clean up animation frames (#1035)
1 parent 33fd808 commit b0a8ad4

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/TabBar.tsx

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ type State = {
6767
tabWidths: { [key: string]: number };
6868
};
6969

70+
type MabyeAnimFrame = number | null;
71+
72+
const cancelAnimFrame = (id: MabyeAnimFrame) => {
73+
if (id !== null) {
74+
cancelAnimationFrame(id);
75+
}
76+
return null;
77+
};
78+
7079
export default class TabBar<T extends Route> extends React.Component<
7180
Props<T>,
7281
State
@@ -121,6 +130,13 @@ export default class TabBar<T extends Route> extends React.Component<
121130
}
122131
}
123132

133+
componentWillUnmount() {
134+
this.cancelAnimFrames();
135+
}
136+
137+
private animFrame1: MabyeAnimFrame = null;
138+
private animFrame2: MabyeAnimFrame = null;
139+
124140
// to store the layout.width of each tab
125141
// when all onLayout's are fired, this would be set in state
126142
private measuredTabWidths: { [key: string]: number } = {};
@@ -129,6 +145,11 @@ export default class TabBar<T extends Route> extends React.Component<
129145

130146
private scrollView: ScrollView | undefined;
131147

148+
private cancelAnimFrames = () => {
149+
this.animFrame1 = cancelAnimFrame(this.animFrame1);
150+
this.animFrame2 = cancelAnimFrame(this.animFrame2);
151+
};
152+
132153
private getFlattenedTabWidth = (style: StyleProp<ViewStyle>) => {
133154
const tabStyle = StyleSheet.flatten(style);
134155

@@ -277,16 +298,19 @@ export default class TabBar<T extends Route> extends React.Component<
277298
// If we don't delay this state update, the UI gets stuck in weird state
278299
// Maybe an issue in Reanimated?
279300
// https://github.com/react-native-community/react-native-tab-view/issues/877
280-
requestAnimationFrame(() =>
281-
requestAnimationFrame(() =>
301+
// Once the above issue is resolved with the nested anim frames this becomes a lot simpler.
302+
// Cancel all pending animations here, since we're going to start 2 new ones shortly.
303+
this.cancelAnimFrames();
304+
this.animFrame1 = requestAnimationFrame(() => {
305+
this.animFrame2 = requestAnimationFrame(() => {
282306
this.setState({
283307
layout: {
284308
height,
285309
width,
286310
},
287-
})
288-
)
289-
);
311+
});
312+
});
313+
});
290314
};
291315

292316
private getTranslateX = memoize(

0 commit comments

Comments
 (0)