@@ -60,7 +60,7 @@ type State = {
6060 scenes : HeaderScene < Route > [ ] ;
6161 progress : ProgressValues ;
6262 layout : Layout ;
63- floatingHeaderHeight : number ;
63+ floatingHeaderHeights : { [ key : string ] : number } ;
6464} ;
6565
6666const dimensions = Dimensions . get ( 'window' ) ;
@@ -92,6 +92,23 @@ const { cond, eq } = Animated;
9292
9393const ANIMATED_ONE = new Animated . Value ( 1 ) ;
9494
95+ const getFloatingHeaderHeights = (
96+ routes : Route [ ] ,
97+ layout : Layout ,
98+ previous : { [ key : string ] : number }
99+ ) => {
100+ const defaultHeaderHeight = getDefaultHeaderHeight ( layout ) ;
101+
102+ return routes . reduce (
103+ ( acc , curr ) => {
104+ acc [ curr . key ] = previous [ curr . key ] || defaultHeaderHeight ;
105+
106+ return acc ;
107+ } ,
108+ { } as { [ key : string ] : number }
109+ ) ;
110+ } ;
111+
95112export default class Stack extends React . Component < Props , State > {
96113 static getDerivedStateFromProps ( props : Props , state : State ) {
97114 if (
@@ -157,6 +174,11 @@ export default class Stack extends React.Component<Props, State> {
157174 } ) ,
158175 progress,
159176 descriptors : props . descriptors ,
177+ floatingHeaderHeights : getFloatingHeaderHeights (
178+ props . routes ,
179+ state . layout ,
180+ state . floatingHeaderHeights
181+ ) ,
160182 } ;
161183 }
162184
@@ -171,7 +193,7 @@ export default class Stack extends React.Component<Props, State> {
171193 // This is not a great heuristic here. We don't know synchronously
172194 // on mount what the header height is so we have just used the most
173195 // common cases here.
174- floatingHeaderHeight : getDefaultHeaderHeight ( layout ) ,
196+ floatingHeaderHeights : { } ,
175197 } ;
176198
177199 private handleLayout = ( e : LayoutChangeEvent ) => {
@@ -186,15 +208,35 @@ export default class Stack extends React.Component<Props, State> {
186208
187209 const layout = { width, height } ;
188210
189- this . setState ( { layout } ) ;
211+ this . setState ( {
212+ layout,
213+ floatingHeaderHeights : getFloatingHeaderHeights (
214+ this . props . routes ,
215+ layout ,
216+ { }
217+ ) ,
218+ } ) ;
190219 } ;
191220
192- private handleFloatingHeaderLayout = ( e : LayoutChangeEvent ) => {
193- const { height } = e . nativeEvent . layout ;
221+ private handleFloatingHeaderLayout = ( {
222+ route,
223+ height,
224+ } : {
225+ route : Route ;
226+ height : number ;
227+ } ) => {
228+ const previousHeight = this . state . floatingHeaderHeights [ route . key ] ;
194229
195- if ( height !== this . state . floatingHeaderHeight ) {
196- this . setState ( { floatingHeaderHeight : height } ) ;
230+ if ( previousHeight && previousHeight === height ) {
231+ return ;
197232 }
233+
234+ this . setState ( state => ( {
235+ floatingHeaderHeights : {
236+ ...state . floatingHeaderHeights ,
237+ [ route . key ] : height ,
238+ } ,
239+ } ) ) ;
198240 } ;
199241
200242 private handleTransitionStart = ( {
@@ -238,7 +280,7 @@ export default class Stack extends React.Component<Props, State> {
238280 onGestureEnd,
239281 } = this . props ;
240282
241- const { scenes, layout, progress, floatingHeaderHeight } = this . state ;
283+ const { scenes, layout, progress, floatingHeaderHeights } = this . state ;
242284
243285 const focusedRoute = navigation . state . routes [ navigation . state . index ] ;
244286 const focusedOptions = descriptors [ focusedRoute . key ] . options ;
@@ -320,7 +362,7 @@ export default class Stack extends React.Component<Props, State> {
320362 onGestureCanceled = { onGestureCanceled }
321363 onGestureEnd = { onGestureEnd }
322364 gestureResponseDistance = { gestureResponseDistance }
323- floatingHeaderHeight = { floatingHeaderHeight }
365+ floatingHeaderHeight = { floatingHeaderHeights [ route . key ] }
324366 hasCustomHeader = { header === null }
325367 getPreviousRoute = { getPreviousRoute }
326368 headerMode = { headerMode }
@@ -348,7 +390,7 @@ export default class Stack extends React.Component<Props, State> {
348390 scenes,
349391 navigation,
350392 getPreviousRoute,
351- onLayout : this . handleFloatingHeaderLayout ,
393+ onContentHeightChange : this . handleFloatingHeaderLayout ,
352394 styleInterpolator :
353395 focusedOptions . headerStyleInterpolator !== undefined
354396 ? focusedOptions . headerStyleInterpolator
0 commit comments