11import { Children , cloneElement , useRef , isValidElement } from 'react' ;
2+ import { useIsoEffect } from './useIsoEffect' ;
23
34export type TransitionGroupProps = {
45 children : React . ReactNode ;
@@ -49,7 +50,7 @@ type VisibleElement = {
4950export function TransitionGroup ( props : TransitionGroupProps ) {
5051 const { children, appear = false , enter = true , exit = false , duration = 500 } = props ;
5152
52- const firstRenderRef = useRef ( true ) ;
53+ const mountedRef = useRef ( false ) ;
5354 const prevVisibleElementsRef = useRef < VisibleElement [ ] > ( [ ] ) ;
5455 const nextVisibleElements : VisibleElement [ ] = [ ] ;
5556 const nextElements : React . ReactElement [ ] = [ ] ;
@@ -71,9 +72,9 @@ export function TransitionGroup(props: TransitionGroupProps) {
7172 const elementClone = cloneElement ( element , {
7273 in : ! removeTimeout ,
7374 enter : false ,
74- appear : firstRenderRef . current ? ( element . props . appear ?? appear ) : ( element . props . enter ?? enter ) ,
7575 exit : element . props . exit ?? exit ,
7676 duration : element . props . duration ?? duration ,
77+ appear : mountedRef . current ? ( element . props . enter ?? enter ) : ( element . props . appear ?? appear ) ,
7778 } ) ;
7879
7980 nextVisibleElements . push ( { element, removeTimeout } ) ;
@@ -128,8 +129,13 @@ export function TransitionGroup(props: TransitionGroupProps) {
128129 addVisibleElement ( derivedElements [ i ] ) ;
129130 }
130131
132+ // Mark as mounted
133+ useIsoEffect ( ( ) => {
134+ mountedRef . current = true ;
135+ } , [ ] ) ;
136+
131137 // Save the visible elements
132138 prevVisibleElementsRef . current = nextVisibleElements ;
133- firstRenderRef . current = false ;
139+
134140 return nextElements ;
135141}
0 commit comments