Skip to content

Commit 0dcb0b2

Browse files
committed
refactor: replace hooks with rc-util useState
1 parent fea90b9 commit 0dcb0b2

File tree

4 files changed

+17
-42
lines changed

4 files changed

+17
-42
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"dependencies": {
4444
"@babel/runtime": "^7.11.1",
4545
"classnames": "^2.2.1",
46-
"rc-util": "^5.18.1"
46+
"rc-util": "^5.19.2"
4747
},
4848
"devDependencies": {
4949
"@types/classnames": "^2.2.9",

src/hooks/useState.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/hooks/useStatus.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import * as React from 'react';
22
import { useRef, useEffect } from 'react';
3+
import useState from 'rc-util/lib/hooks/useState';
34
import {
45
STATUS_APPEAR,
56
STATUS_NONE,
6-
MotionStatus,
77
STATUS_LEAVE,
88
STATUS_ENTER,
9-
MotionEventHandler,
109
STEP_PREPARE,
1110
STEP_START,
1211
STEP_ACTIVE,
12+
} from '../interface';
13+
import type {
14+
MotionStatus,
15+
MotionEventHandler,
1316
MotionEvent,
1417
MotionPrepareEventHandler,
1518
StepStatus,
1619
} from '../interface';
17-
import useState from './useState';
18-
import { CSSMotionProps } from '../CSSMotion';
20+
import type { CSSMotionProps } from '../CSSMotion';
1921
import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';
2022
import useStepQueue, { DoStep, SkipStep, isActive } from './useStepQueue';
2123
import useDomMotionEvents from './useDomMotionEvents';
@@ -52,7 +54,6 @@ export default function useStatus(
5254

5355
const mountedRef = useRef(false);
5456
const deadlineRef = useRef(null);
55-
const destroyedRef = useRef(false);
5657

5758
// =========================== Dom Node ===========================
5859
const cacheElementRef = useRef<HTMLElement>(null);
@@ -85,9 +86,9 @@ export default function useStatus(
8586
}
8687

8788
// Only update status when `canEnd` and not destroyed
88-
if (canEnd !== false && !destroyedRef.current) {
89-
setStatus(STATUS_NONE);
90-
setStyle(null);
89+
if (canEnd !== false) {
90+
setStatus(STATUS_NONE, true);
91+
setStyle(null, true);
9192
}
9293
}
9394

@@ -126,7 +127,7 @@ export default function useStatus(
126127
}
127128
}, [status]);
128129

129-
const [startStep, step] = useStepQueue(status, (newStep) => {
130+
const [startStep, step] = useStepQueue(status, newStep => {
130131
// Only prepare step can be skip
131132
if (newStep === STEP_PREPARE) {
132133
const onPrepare = eventHandlers[STEP_PREPARE];
@@ -219,7 +220,6 @@ export default function useStatus(
219220
useEffect(
220221
() => () => {
221222
clearTimeout(deadlineRef.current);
222-
destroyedRef.current = true;
223223
},
224224
[],
225225
);

src/hooks/useStepQueue.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import * as React from 'react';
2-
import type {
3-
StepStatus,
4-
MotionStatus} from '../interface';
2+
import useState from 'rc-util/lib/hooks/useState';
3+
import type { StepStatus, MotionStatus } from '../interface';
54
import {
65
STEP_PREPARE,
76
STEP_ACTIVE,
87
STEP_START,
98
STEP_ACTIVATED,
10-
STEP_NONE
9+
STEP_NONE,
1110
} from '../interface';
1211
import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';
1312
import useNextFrame from './useNextFrame';
14-
import useState from './useState';
1513

1614
const STEP_QUEUE: StepStatus[] = [
1715
STEP_PREPARE,
@@ -40,7 +38,7 @@ export default (
4038
const [nextFrame, cancelNextFrame] = useNextFrame();
4139

4240
function startQueue() {
43-
setStep(STEP_PREPARE);
41+
setStep(STEP_PREPARE, true);
4442
}
4543

4644
useIsomorphicLayoutEffect(() => {
@@ -52,15 +50,15 @@ export default (
5250

5351
if (result === SkipStep) {
5452
// Skip when no needed
55-
setStep(nextStep);
53+
setStep(nextStep, true);
5654
} else {
5755
// Do as frame for step update
5856
nextFrame(info => {
5957
function doNext() {
6058
// Skip since current queue is ood
6159
if (info.isCanceled()) return;
6260

63-
setStep(nextStep);
61+
setStep(nextStep, true);
6462
}
6563

6664
if (result === true) {

0 commit comments

Comments
 (0)