In my react-native project, I am trying to have my spinner rotating. My spinner is a SVG component <Spinner />. It is like this:
import * as React from 'react';
import Svg, {Path} from 'react-native-svg';
function Spinner(props) {
return (
<Svg width={24} height={24} fill="none" {...props}>
<Path
...
/>
</Svg>
);
}
export default Spinner;
Since it is a static SVG element, my plan is to create a component that can have rotating animation & apply it to any screens which needs a loading spinner. So I firstly created a LoadingSpinner component which is supposed to have the rotation animation with the <Spinner />:
import React, {Component, useState, useEffect} from 'react';
import {View, Animated, Easing} from 'react-native';
import Spinner from './Spinner';
const LoadingSpinner = () => {
const [spinAnim, setSpinAnim] = useState(new Animated.Value(0));
useEffect(() => {
Animated.loop(
Animated.timing(spinAnim, {
toValue: 1,
duration: 3000,
easing: Easing.linear,
useNativeDriver: true,
}),
).start();
});
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Animated.View>
<Spinner />
</Animated.View>
</View>
);
};
export default LoadingSpinner;
Then, in my screen that needs a rotating spinner I just render the LoadingSpinner,
import LoadingSpinner from '../component/LoadingSpinner'
...
const MyScreen = () => {
...
return (
<View>
{status==='loading' ? <LoadingSpinner /> : null}
</View>
)
}
When I run my app, I can see the spinner is rendered on the screen but it is not rotating. What am I missing?