Skip to content

Instantly share code, notes, and snippets.

@mattThousand
Created January 4, 2021 02:54
Show Gist options
  • Select an option

  • Save mattThousand/afa25f5a4e8091689700b96d8056dd80 to your computer and use it in GitHub Desktop.

Select an option

Save mattThousand/afa25f5a4e8091689700b96d8056dd80 to your computer and use it in GitHub Desktop.
import React, {FunctionComponent, useEffect} from 'react';
import {Animated, Dimensions, FlatList, StyleSheet, View} from 'react-native';
const {width} = Dimensions.get('screen');
const AnimatedProgress: FunctionComponent = () => {
return (
<FlatList
contentContainerStyle={style.contentContainer}
data={[1, 2, 3, 4, 5]}
keyExtractor={(_, index) => index.toString()}
renderItem={({item}) => <ProgressBar widthPct={item} />}
/>
);
};
const ProgressBar: FunctionComponent<{widthPct: number}> = ({widthPct}) => {
const barWidth = React.useRef(new Animated.Value(0)).current;
const finalWidth = (width * widthPct) / 10;
useEffect(() => {
Animated.spring(barWidth, {
toValue: finalWidth,
bounciness: 10,
speed: 2,
useNativeDriver: true,
delay: widthPct * 100,
}).start();
}, []);
return (
<View style={style.barContainer}>
<Animated.View style={[style.progressBar, {width: barWidth}]} />
</View>
);
};
const style = StyleSheet.create({
contentContainer: {
flex: 1,
justifyContent: 'center',
padding: 30,
},
barContainer: {
padding: 40,
},
progressBar: {
backgroundColor: 'purple',
height: 30,
borderRadius: 15,
},
});
export default AnimatedProgress;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment