import React, { useState, useEffect } from 'react'; import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'; interface Lap { id: number; time: number; } export default function App() { const [time, setTime] = useState(0); const [running, setRunning] = useState(false); const [laps, setLaps] = useState([]); useEffect(() => { let interval: NodeJS.Timeout | null = null; if (running) { interval = setInterval(() => { setTime((prevTime) => prevTime + 10); }, 10); } else { if (interval) clearInterval(interval); } return () => { if (interval) clearInterval(interval); }; }, [running]); const startStopwatch = () => { setRunning(true); }; const stopStopwatch = () => { setRunning(false); }; const resetStopwatch = () => { setTime(0); setLaps([]); setRunning(false); }; const lapTime = () => { setLaps((prevLaps) => [...prevLaps, { id: prevLaps.length + 1, time }]); }; const formatTime = (time: number) => { const minutes = Math.floor((time / 60000) % 60); const seconds = Math.floor((time / 1000) % 60); const milliseconds = Math.floor((time % 1000) / 10); return ( (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds + ':' + (milliseconds < 10 ? '0' : '') + milliseconds ); }; return ( {formatTime(time)} {laps.length > 0 && ( Laps: {laps.map((lap) => ( Lap {lap.id}: {formatTime(lap.time)} ))} )} {running ? 'Stop' : 'Start'} Lap Reset ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, timer: { fontSize: 50, marginBottom: 20, }, lapsContainer: { alignItems: 'center', marginBottom: 20, }, lapHeading: { fontSize: 20, fontWeight: 'bold', marginBottom: 10, }, lapTime: { fontSize: 16, marginBottom: 5, }, buttonsContainer: { flexDirection: 'row', }, button: { marginHorizontal: 10, paddingVertical: 10, paddingHorizontal: 20, backgroundColor: '#007bff', borderRadius: 5, }, buttonText: { color: 'white', fontSize: 18, }, });