import React, { useState, useEffect } from 'react';
import { View, Text, Button, FlatList, ActivityIndicator, StyleSheet } from 'react-native';
export default function PerformanceImpactDemo() {
const [isSlow, setIsSlow] = useState(true);
const [loading, setLoading] = useState(true);
const [data, setData] = useState([]);
useEffect(() => {
setLoading(true);
setData([]);
if (isSlow) {
const timer = setTimeout(() => {
setData(['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']);
setLoading(false);
}, 3000);
return () => clearTimeout(timer);
} else {
setData(['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']);
setLoading(false);
}
}, [isSlow]);
return (
<View style={styles.container}>
<Text style={styles.title}>Performance Impact Demo</Text>
<View style={styles.toggleContainer}>
<Button title={isSlow ? 'Switch to Fast Load' : 'Switch to Slow Load'} onPress={() => setIsSlow(!isSlow)} />
</View>
<View style={styles.content}>
{loading ? (
<ActivityIndicator size="large" color="#0000ff" accessibilityLabel="Loading indicator" />
) : (
<FlatList
data={data}
keyExtractor={(item) => item}
renderItem={({ item }) => <Text style={styles.item}>{item}</Text>}
accessibilityLabel="List of items"
/>
)}
</View>
<Text style={styles.note} accessibilityRole="text">
{isSlow
? 'Slow loading frustrates users and may cause them to leave your app early.'
: 'Fast loading keeps users happy and encourages them to stay longer.'}
</Text>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 20, backgroundColor: '#fff' },
title: { fontSize: 24, fontWeight: 'bold', marginBottom: 20 },
toggleContainer: { marginBottom: 20 },
content: { flex: 1 },
item: { fontSize: 18, paddingVertical: 8 },
note: { marginTop: 20, fontSize: 16, color: '#555' }
});This React Native screen demonstrates how app performance affects user retention by letting users toggle between slow and fast loading modes.
We use useEffect to simulate loading data. When in slow mode, we delay showing the list by 3 seconds using setTimeout. In fast mode, data appears immediately.
While loading, an ActivityIndicator shows to inform users the app is working. After loading, a list of 5 items appears.
The text below explains why slow loading can cause users to leave, helping learners connect performance with user experience.