RefreshControl في React Native
يُستخدَم هذا المكوّن داخل مكوّن ScrollView أو مكوّن ListView لإضافة وظيفة التحديث بالسحب. عندما يكون المكوّن ScrollView في الموقع scrollY: 0، يؤدي السحب لأسفل إلى تشغيل حَدَث onRefresh.
مثال
import React from 'react';
import {
ScrollView,
RefreshControl,
StyleSheet,
Text,
SafeAreaView,
} from 'react-native';
import Constants from 'expo-constants';
function wait(timeout) {
return new Promise(resolve => {
setTimeout(resolve, timeout);
});
}
export default function App() {
const [refreshing, setRefreshing] = React.useState(false);
const onRefresh = React.useCallback(() => {
setRefreshing(true);
wait(2000).then(() => setRefreshing(false));
}, [refreshing]);
return (
<SafeAreaView style={styles.container}>
<ScrollView
contentContainerStyle={styles.scrollView}
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
}
>
<Text>Pull down to see RefreshControl indicator</Text>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
},
scrollView: {
flex: 1,
backgroundColor: 'pink',
alignItems: 'center',
justifyContent: 'center',
},
});
ملاحظة: الخاصيّة refreshing مُتحكم فيها (controlled prop)، ولهذا السبب يجب تعيين القيمة true لها في دالة onRefresh وإلا سيتوقف مؤشر التحديث فورًا.
الخاصيات
يرث خاصيّات المكوّن View.
refreshing
ما إذا كان على العرض أن يشير إلى تحديث قيد العمل.
| النوع | مطلوب |
|---|---|
| قيمة منطقيّة | نعم |
onRefresh
دالةٌ تُستدعى عندما يبدأ العرض بالتحديث.
| النوع | مطلوب |
|---|---|
| دالة | لا |
colors
الألوان (واحد على الأقل) التي ستُستخدَم لرسم مؤشر التحديث.
| النوع | مطلوب | المنصة |
|---|---|---|
| مصفوفة ألوان | لا | Android |
enabled
ما إذا تم تمكين وظيفة التحديث بالسحب.
| النوع | مطلوب | المنصة |
|---|---|---|
| قيمة منطقية | لا | Android |
progressBackgroundColor
لون خلفية مؤشر التحديث.
| النوع | مطلوب | المنصة |
|---|---|---|
| لون | لا | Android |
progressViewOffset
الإزاحة العلويّة لعرض التقدم.
| النوع | مطلوب | المنصة |
|---|---|---|
| عدد | لا | Android |
size
حجم مؤشر التحديث، راجع RefreshControl.SIZE.
| النوع | مطلوب | المنصة |
|---|---|---|
enum(RefreshLayoutConsts.SIZE.DEFAULT, RefreshLayoutConsts.SIZE.LARGE)
|
لا | Android |
tintColor
لون مؤشر التحديث.
| النوع | مطلوب | المنصة |
|---|---|---|
| لون | لا | iOS |
title
العنوان المعروض تحت مؤشر التحديث.
| النوع | مطلوب | المنصة |
|---|---|---|
| سلسلة نصيّة | لا | iOS |
titleColor
لون العنوان.
| النوع | مطلوب | المنصة |
|---|---|---|
| لون | لا | iOS |