11import { isArray , isNumber } from 'lodash' ;
2- import { useEffect , useRef , useState } from 'react' ;
2+ import { useEffect , useMemo , useRef } from 'react' ;
33
44import { useAsync , useForceUpdate , useIsomorphicLayoutEffect } from '@react-devui/hooks' ;
55
@@ -38,14 +38,16 @@ export function DTransition(props: DTransitionProps): JSX.Element | null {
3838 afterLeave,
3939 } = props ;
4040
41- const [ initState ] = useState ( ( ) => {
41+ const initState = useMemo ( ( ) => {
4242 const [ skipEnter , skipLeave ] = isArray ( dSkipFirstTransition ) ? dSkipFirstTransition : [ dSkipFirstTransition , dSkipFirstTransition ] ;
4343 if ( dIn ) {
4444 return skipEnter ? T_ENTERED : T_BEFORE_ENTER ;
4545 } else {
4646 return skipLeave ? T_LEAVED : T_BEFORE_LEAVE ;
4747 }
48- } ) ;
48+ // eslint-disable-next-line react-hooks/exhaustive-deps
49+ } , [ ] ) ;
50+
4951 const dataRef = useRef < {
5052 prevIn : boolean ;
5153 state : DTransitionState ;
@@ -71,33 +73,31 @@ export function DTransition(props: DTransitionProps): JSX.Element | null {
7173 dataRef . current . isFirstEnter = false ;
7274 }
7375
76+ const state = dataRef . current . state ;
77+ const startTransition = state === T_ENTER || state === T_LEAVE ;
78+
7479 useIsomorphicLayoutEffect ( ( ) => {
75- if ( dataRef . current . state === T_ENTERED ) {
80+ if ( state === T_ENTERED ) {
7681 onEnterRendered ?.( ) ;
7782 }
7883 // eslint-disable-next-line react-hooks/exhaustive-deps
7984 } , [ ] ) ;
8085
8186 useIsomorphicLayoutEffect ( ( ) => {
82- if ( dataRef . current . state === T_BEFORE_ENTER ) {
87+ if ( state === T_BEFORE_ENTER ) {
8388 onEnterRendered ?.( ) ;
8489 updateTransitionState ( T_ENTER ) ;
85- } else if ( dataRef . current . state === T_BEFORE_LEAVE ) {
90+ } else if ( state === T_BEFORE_LEAVE ) {
8691 updateTransitionState ( T_LEAVE ) ;
8792 }
8893 // eslint-disable-next-line react-hooks/exhaustive-deps
8994 } , [ dIn ] ) ;
9095
9196 useEffect ( ( ) => {
92- dataRef . current . clearTid ?.( ) ;
93-
94- const isEnter = dataRef . current . state === T_ENTER ;
95- if ( dataRef . current . state === T_ENTER || dataRef . current . state === T_LEAVE ) {
96- if ( isEnter ) {
97- onEnterRendered ?.( ) ;
98- }
99-
97+ if ( startTransition ) {
98+ dataRef . current . clearTid ?.( ) ;
10099 dataRef . current . clearTid = asyncCapture . requestAnimationFrame ( ( ) => {
100+ const isEnter = state === T_ENTER ;
101101 const nextState = isEnter ? T_ENTERING : T_LEAVING ;
102102 updateTransitionState ( nextState ) ;
103103
@@ -111,7 +111,7 @@ export function DTransition(props: DTransitionProps): JSX.Element | null {
111111 } ) ;
112112 }
113113 // eslint-disable-next-line react-hooks/exhaustive-deps
114- } , [ dIn ] ) ;
114+ } , [ startTransition ] ) ;
115115
116116 const shouldRender = ( ( ) => {
117117 if ( dataRef . current . isFirstEnter && ! dMountBeforeFirstEnter ) {
@@ -121,5 +121,5 @@ export function DTransition(props: DTransitionProps): JSX.Element | null {
121121 return true ;
122122 } ) ( ) ;
123123
124- return shouldRender ? children ( dataRef . current . state ) : null ;
124+ return shouldRender ? children ( state ) : null ;
125125}
0 commit comments