Skip to content

Commit 2636550

Browse files
committed
Update useTransitionAnimation.stories.tsx
1 parent fdf1beb commit 2636550

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

stories/hooks/useTransitionAnimation.stories.tsx

+12-10
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ import {
88

99
export default { component: useTransitionAnimation };
1010

11-
const Text = ({ children }: { children: string }) => {
12-
const prev = useRef(children);
11+
const usePrevious = <T,>(value: T) => {
12+
const prev = useRef(value);
1313
useEffect(() => {
14-
prev.current = children;
15-
}, [children]);
14+
prev.current = value;
15+
}, [value]);
16+
return prev.current;
17+
};
18+
19+
const Text = ({ children }: { children: string }) => {
20+
const prev = usePrevious(children);
1621

1722
const timing = { duration: 800, easing: "ease-out" };
1823
const animate = useTransitionAnimation({
@@ -32,7 +37,7 @@ const Text = ({ children }: { children: string }) => {
3237
),
3338
update: useAnimation(
3439
[{ transform: "rotateX(360deg)" }, { transform: "rotateX(0deg)" }],
35-
children !== prev.current ? timing : undefined
40+
children !== prev ? timing : undefined
3641
),
3742
});
3843

@@ -76,15 +81,12 @@ const shuffle = <T,>(array: T[]): T[] => {
7681

7782
const SvgText = ({ children, i }: { children: string; i: number }) => {
7883
const x = i * 20;
79-
const prevX = useRef(x);
80-
useEffect(() => {
81-
prevX.current = x;
82-
}, [x]);
84+
const prevX = usePrevious(x);
8385
const timing = { duration: 800, easing: "ease-in-out" };
8486
const transition = useTransitionAnimation({
8587
update: useAnimation(
8688
[
87-
{ transform: `translateX(${prevX.current - x}px)` },
89+
{ transform: `translateX(${prevX - x}px)` },
8890
{ transform: `translateX(0px)` },
8991
],
9092
timing

0 commit comments

Comments
 (0)