@@ -19,6 +19,7 @@ import * as React from "react";
19
19
import { CSSTransition , TransitionGroup } from "react-transition-group" ;
20
20
21
21
import { Classes , DISPLAYNAME_PREFIX , type Props } from "../../common" ;
22
+ import { usePrevious } from "../../hooks" ;
22
23
23
24
import type { Panel } from "./panelTypes" ;
24
25
import { PanelView2 } from "./panelView2" ;
@@ -97,21 +98,14 @@ export const PanelStack2: PanelStack2Component = <T extends Panel<object>>(props
97
98
stack : propsStack ,
98
99
} = props ;
99
100
const isControlled = propsStack != null ;
100
- const [ direction , setDirection ] = React . useState ( "push" ) ;
101
101
102
102
const [ localStack , setLocalStack ] = React . useState < T [ ] > ( initialPanel !== undefined ? [ initialPanel ] : [ ] ) ;
103
103
const stack = React . useMemo (
104
104
( ) => ( isControlled ? propsStack . slice ( ) . reverse ( ) : localStack ) ,
105
105
[ localStack , isControlled , propsStack ] ,
106
106
) ;
107
- const stackLength = React . useRef < number > ( stack . length ) ;
108
- React . useEffect ( ( ) => {
109
- if ( stack . length !== stackLength . current ) {
110
- // Adjust the direction in case the stack size has changed, controlled or uncontrolled
111
- setDirection ( stack . length - stackLength . current < 0 ? "pop" : "push" ) ;
112
- }
113
- stackLength . current = stack . length ;
114
- } , [ stack ] ) ;
107
+ const prevStackLength = usePrevious ( stack . length ) ?? stack . length ;
108
+ const direction = stack . length - prevStackLength < 0 ? "pop" : "push" ;
115
109
116
110
const handlePanelOpen = React . useCallback (
117
111
( panel : T ) => {
0 commit comments