Skip to content

Commit ed8d3cf

Browse files
authored
Fixed rare case of infinitive animation of animated image (#3332)
* Fixed rare case of infinitive animation of animated image * Fixed tests
1 parent 59b71f6 commit ed8d3cf

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

demo/src/screens/__tests__/__snapshots__/AvatarScreen.spec.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,7 @@ exports[`AvatarScreen renders screen 1`] = `
14501450
assetGroup="icons"
14511451
onError={[Function]}
14521452
onLoad={[Function]}
1453+
onLoadStart={[Function]}
14531454
source={
14541455
{
14551456
"uri": "https://static.pexels.com/photos/60628/flower-garden-blue-sky-hokkaido-japan-60628.jpeg",

src/components/animatedImage/index.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ const AnimatedImage = (props: AnimatedImageProps) => {
3636
loader,
3737
style,
3838
onLoad: propsOnLoad,
39+
onLoadStart: propsOnLoadStart,
3940
animationDuration = 300,
4041
testID,
4142
...others
4243
} = props;
4344

44-
const [isLoading, setIsLoading] = useState(true);
45+
const [isLoading, setIsLoading] = useState(false);
4546
const opacity = useSharedValue(0);
4647

4748
useDidUpdate(() => {
@@ -50,16 +51,23 @@ const AnimatedImage = (props: AnimatedImageProps) => {
5051
}
5152
}, [loader]);
5253

53-
const onLoad = useCallback((event: NativeSyntheticEvent<ImageLoadEventData>) => {
54-
setIsLoading(false);
55-
propsOnLoad?.(event);
56-
// did not start the animation already
57-
if (opacity.value === 0) {
58-
opacity.value = withTiming(1, {duration: animationDuration});
59-
}
60-
},
61-
// eslint-disable-next-line react-hooks/exhaustive-deps
62-
[setIsLoading, propsOnLoad, animationDuration]);
54+
const onLoad = useCallback(
55+
(event: NativeSyntheticEvent<ImageLoadEventData>) => {
56+
setIsLoading(false);
57+
propsOnLoad?.(event);
58+
// did not start the animation already
59+
if (opacity.value === 0) {
60+
opacity.value = withTiming(1, {duration: animationDuration});
61+
}
62+
},
63+
// eslint-disable-next-line react-hooks/exhaustive-deps
64+
[setIsLoading, propsOnLoad, animationDuration]
65+
);
66+
67+
const onLoadStart = useCallback(() => {
68+
setIsLoading(true);
69+
propsOnLoadStart?.();
70+
}, [setIsLoading, propsOnLoadStart, animationDuration]);
6371

6472
const fadeInStyle = useAnimatedStyle(() => {
6573
return {opacity: opacity.value};
@@ -75,6 +83,7 @@ const AnimatedImage = (props: AnimatedImageProps) => {
7583
style={_style}
7684
source={source}
7785
onLoad={onLoad}
86+
onLoadStart={onLoadStart}
7887
testID={testID}
7988
imageStyle={undefined}
8089
/>

0 commit comments

Comments
 (0)