Files
Michał Osadnik 4408906731 Clear timeout if needed send event less often (#163)
I think it's more natural if animation do not perform so often. Once per second looks better imho and it's a better example of this library usage. 

Also, removed animations if "fetch" is finished
2019-02-05 12:23:43 +01:00

74 lines
1.5 KiB
JavaScript

import React, { Component } from 'react';
import { StyleSheet, View, Button } from 'react-native';
import ProgressBar from './ProgressBar';
export default class Progressable extends Component {
static navigationOptions = {
title: 'ProgressBar Example',
};
state = {
progress: 0,
visible: true,
};
fetchData = () => {
let progress = 0;
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = setInterval(() => {
this.setState({
progress,
});
progress += 0.1;
if (progress > 1) {
clearInterval(this.timeout);
}
}, 1000);
};
componentDidMount() {
this.fetchData();
}
render() {
return (
<View style={styles.container}>
{this.state.visible && (
<ProgressBar progress={this.state.progress} style={{ margin: 20 }} />
)}
<Button
onPress={() => {
this.setState({ progress: 0 });
this.fetchData();
}}
title="RESET"
/>
<Button
onPress={() => this.setState(prev => ({ visible: !prev.visible }))}
title="TOGGLE VISIBILITY"
/>
</View>
);
}
}
const BOX_SIZE = 100;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
box: {
width: BOX_SIZE,
height: BOX_SIZE,
borderColor: '#F5FCFF',
alignSelf: 'center',
backgroundColor: 'plum',
margin: BOX_SIZE / 2,
},
});