Skip to content

Commit 42f54bc

Browse files
committed
fix: Motion should not unmount node
1 parent af8c89e commit 42f54bc

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

examples/CSSMotion.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ async function forceDelay(): Promise<void> {
1717
});
1818
}
1919

20+
const Div = React.forwardRef<HTMLDivElement, any>((props, ref) => {
21+
React.useEffect(() => {
22+
console.log('DIV >>> Mounted!');
23+
24+
return () => {
25+
console.log('DIV >>> UnMounted!');
26+
};
27+
}, []);
28+
29+
return <div {...props} ref={ref} />;
30+
});
31+
2032
class Demo extends React.Component<{}, DemoState> {
2133
state: DemoState = {
2234
show: true,
@@ -27,7 +39,9 @@ class Demo extends React.Component<{}, DemoState> {
2739
};
2840

2941
onTrigger = () => {
30-
this.setState(({ show }) => ({ show: !show }));
42+
setTimeout(() => {
43+
this.setState(({ show }) => ({ show: !show }));
44+
}, 100);
3145
};
3246

3347
onTriggerDelay = () => {
@@ -128,7 +142,7 @@ class Demo extends React.Component<{}, DemoState> {
128142
onLeaveEnd={this.skipColorTransition}
129143
>
130144
{({ style, className }, ref) => (
131-
<div
145+
<Div
132146
ref={ref}
133147
className={classNames('demo-block', className)}
134148
style={style}

src/CSSMotion.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export function genCSSMotion(
134134
}
135135
}
136136

137-
const [status, statusStep, statusStyle] = useStatus(
137+
const [status, statusStep, statusStyle, mergedVisible] = useStatus(
138138
supportMotion,
139139
visible,
140140
getDomElement,
@@ -159,7 +159,7 @@ export function genCSSMotion(
159159
motionChildren = null;
160160
} else if (status === STATUS_NONE || !isSupportTransition(props)) {
161161
// Stable children
162-
if (visible) {
162+
if (mergedVisible) {
163163
motionChildren = children({ ...eventProps }, setNodeRef);
164164
} else if (!removeOnLeave) {
165165
motionChildren = children(

src/hooks/useStatus.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export default function useStatus(
4343
onEnterEnd,
4444
onLeaveEnd,
4545
}: CSSMotionProps,
46-
): [MotionStatus, StepStatus, React.CSSProperties] {
46+
): [MotionStatus, StepStatus, React.CSSProperties, boolean] {
47+
// Used for outer render usage to avoid `visible: false & status: none` to render nothing
48+
const [asyncVisible, setAsyncVisible] = useState(visible);
4749
const [status, setStatus] = useState<MotionStatus>(STATUS_NONE);
4850
const [style, setStyle] = useState<React.CSSProperties | undefined>(null);
4951

@@ -192,6 +194,8 @@ export default function useStatus(
192194
setStatus(nextStatus);
193195
startStep();
194196
}
197+
198+
setAsyncVisible(visible);
195199
}, [visible]);
196200

197201
// ============================ Effect ============================
@@ -225,5 +229,5 @@ export default function useStatus(
225229
};
226230
}
227231

228-
return [status, step, mergedStyle];
232+
return [status, step, mergedStyle, asyncVisible];
229233
}

0 commit comments

Comments
 (0)