Просто создайте чистый компонент, как показано ниже:
Loader.js
import {ActivityIndicator, Modal, Text, View} from "react-native";
import React, {PureComponent} from 'react';
class Loader extends PureComponent {
static propTypes = {
onCancel: PropTypes.func,
isLoading : PropTypes.bool
};
render() {
return <Modal
animated={true}
visible={this.props.isLoading}
transparent={true}
animationType={'fade'}>
<View style={{justifyContent: 'center', alignItems: 'center', flex: 1}}>
<View style={{
width: 120, height: 40, borderRadius: 5, padding: 10, backgroundColor: 'lightcoral',
justifyContent: 'center', alignItems: 'center', flexDirection: 'row'
}}>
<ActivityIndicator
color='white'
size={'small'}
animating={true}/>
<Text style={{padding: 8, color: 'white'}}>Loading</Text>
</View>
<Button title={'Cancel'} onPress={this.props.onCancel}/>
</View>
</Modal>;
}
}
export default Loader
Импортируйте этот файл и используйте загрузчик в любом .js
, используя следующую команду:
import Loader from "./source/Loader";
<Loader isLoading={this.state.isLoading} onCancel={this._cancelProgress}/>
Чтобы вручную обрабатывать функцию отмены, вы должны использовать функцию обратного вызова.
вы должны определить метод cancelProgress в вашем файле .js, чтобы изменить состояние и передать его в файл loader.js
аналогично приведенному выше примеру.
_cancelProgress = () => {
this.setState({
isLoading: false
});
}
Измените состояние isLoading
на true / false, чтобы показать и скрыть ActivityIndicator
.
this.setState({isLoading: false})
.