Code Detail
Prompt
import React, { useState, useEffect } from 'react';
import { View, Text, Switch, StyleSheet } from 'react-native';
export default function SmartHomeEnergySaver() {
const [isEnergySaving, setIsEnergySaving] = useState(false);
const [energyUsage, setEnergyUsage] = useState(100); // Default energy usage
useEffect(() => {
if (isEnergySaving) {
setEnergyUsage(prevUsage => Math.max(prevUsage * 0.8, 10)); // Reduce energy by 20%, but keep a minimum
} else {
setEnergyUsage(100); // Reset when mode is off
}
}, [isEnergySaving]);
return (
<View style={styles.container}>
<Text style={styles.title}>Smart Home Energy Saver</Text>
<Text style={styles.status}>Energy Saving Mode: {isEnergySaving ? 'ON' : 'OFF'}</Text>
<Switch
value={isEnergySaving}
onValueChange={setIsEnergySaving}
/>
<Text style={styles.energyUsage}>Current Energy Usage: {energyUsage.toFixed(2)} kWh</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f8f9fa',
padding: 20,
},
title: {
fontSize: 24,
fontWeight: 'bold',
color: '#0f172a',
marginBottom: 20,
},
status: {
fontSize: 18,
color: '#64748b',
marginBottom: 10,
},
energyUsage: {
fontSize: 16,
color: '#64748b',
marginTop: 20,
},
});