Skip to content

Commit 006b3da

Browse files
authored
chore: unmount loop (#456)
1 parent 68d8dd8 commit 006b3da

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/elements/Skeleton/Skeleton.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@ import { Text, TextProps } from "../Text"
1616
*/
1717
export const Skeleton: FC<{ children: ReactNode }> = ({ children }) => {
1818
const opacity = useRef(new Animated.Value(0.5))
19+
const animationRef = useRef<Animated.CompositeAnimation | null>(null)
1920

2021
useEffect(() => {
21-
Animated.loop(
22+
animationRef.current = Animated.loop(
2223
Animated.timing(opacity.current, {
2324
toValue: 1,
2425
duration: 1000,
2526
useNativeDriver: true,
2627
})
27-
).start()
28+
)
29+
30+
animationRef.current.start()
31+
32+
return () => {
33+
animationRef.current.stop()
34+
}
2835
}, [])
2936

3037
return (

src/elements/Spinner/Spinner.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useMemo } from "react"
1+
import { useEffect, useMemo, useRef } from "react"
22
import { Animated, Easing, ViewProps } from "react-native"
33
import styled from "styled-components/native"
44
import { Color } from "../../types"
@@ -58,9 +58,10 @@ export const Spinner: React.FC<SpinnerProps> = ({
5858
}) => {
5959
const color = useColor()
6060
const rotation = useMemo(() => new Animated.Value(0), [])
61+
const animationRef = useRef<Animated.CompositeAnimation | null>(null)
6162

62-
const startRotation = () => {
63-
Animated.loop(
63+
useEffect(() => {
64+
animationRef.current = Animated.loop(
6465
Animated.timing(rotation, {
6566
toValue: 1,
6667
duration: 1000,
@@ -70,11 +71,13 @@ export const Spinner: React.FC<SpinnerProps> = ({
7071
{
7172
iterations: -1,
7273
}
73-
).start()
74-
}
74+
)
7575

76-
useEffect(() => {
77-
startRotation()
76+
animationRef.current.start()
77+
78+
return () => {
79+
animationRef.current.stop()
80+
}
7881
}, [])
7982

8083
const style = [

0 commit comments

Comments
 (0)