@@ -67,13 +67,12 @@ type State = {
6767 tabWidths : { [ key : string ] : number } ;
6868} ;
6969
70- type MabyeAnimFrame = number | null ;
70+ const scheduleInNextFrame = ( cb : ( ) => void ) => {
71+ let frame = requestAnimationFrame ( ( ) => {
72+ frame = requestAnimationFrame ( cb ) ;
73+ } ) ;
7174
72- const cancelAnimFrame = ( id : MabyeAnimFrame ) => {
73- if ( id !== null ) {
74- cancelAnimationFrame ( id ) ;
75- }
76- return null ;
75+ return ( ) => cancelAnimationFrame ( frame ) ;
7776} ;
7877
7978export default class TabBar < T extends Route > extends React . Component <
@@ -131,12 +130,9 @@ export default class TabBar<T extends Route> extends React.Component<
131130 }
132131
133132 componentWillUnmount ( ) {
134- this . cancelAnimFrames ( ) ;
133+ this . cancelNextFrameCb ?. ( ) ;
135134 }
136135
137- private animFrame1 : MabyeAnimFrame = null ;
138- private animFrame2 : MabyeAnimFrame = null ;
139-
140136 // to store the layout.width of each tab
141137 // when all onLayout's are fired, this would be set in state
142138 private measuredTabWidths : { [ key : string ] : number } = { } ;
@@ -145,10 +141,7 @@ export default class TabBar<T extends Route> extends React.Component<
145141
146142 private scrollView : ScrollView | undefined ;
147143
148- private cancelAnimFrames = ( ) => {
149- this . animFrame1 = cancelAnimFrame ( this . animFrame1 ) ;
150- this . animFrame2 = cancelAnimFrame ( this . animFrame2 ) ;
151- } ;
144+ private cancelNextFrameCb : ( ( ) => void ) | undefined = undefined ;
152145
153146 private getFlattenedTabWidth = ( style : StyleProp < ViewStyle > ) => {
154147 const tabStyle = StyleSheet . flatten ( style ) ;
@@ -298,19 +291,16 @@ export default class TabBar<T extends Route> extends React.Component<
298291 // If we don't delay this state update, the UI gets stuck in weird state
299292 // Maybe an issue in Reanimated?
300293 // https://github.com/react-native-community/react-native-tab-view/issues/877
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 ( ( ) => {
306- this . setState ( {
307- layout : {
308- height,
309- width,
310- } ,
311- } ) ;
312- } ) ;
313- } ) ;
294+ // Cancel any pending callbacks, since we're scheduling a new one
295+ this . cancelNextFrameCb ?.( ) ;
296+ this . cancelNextFrameCb = scheduleInNextFrame ( ( ) =>
297+ this . setState ( {
298+ layout : {
299+ height,
300+ width,
301+ } ,
302+ } )
303+ ) ;
314304 } ;
315305
316306 private getTranslateX = memoize (
0 commit comments