I have recently started working on react native, and I am currently working on the below code for timer and countdown functionality. Before I am able to put specific time and it is working fine. Also I am able to increment and decrement static values, but on timer getting error.
In the below code while incrementing and decrementing the time, I am getting 'NaN' as output.
//Timing.js
export const Timing = ({ onChangeTime }) => {
return (
<>
<View style={styles.timingButton}>
<RoundedButton size={75} title=" " onPress={() => onChangeTime(onChangeTime 1)} />
</View>
<View style={styles.timingButton}>
<RoundedButton size={75} title="15" onPress={() => onChangeTime(15)} />
</View>
<View style={styles.timingButton}>
<RoundedButton size={75} title="-" onPress={() => onChangeTime(onChangeTime -1)} />
</View>
</>
);
};
//Timer.js
export const Timer = ({ focusSubject, clearSubject, onTimerEnd }) => {
useKeepAwake();
const [isStarted, setIsStarted] = useState(false);
const [progress, setProgress] = useState(1);
const [minutes, setMinutes] = useState(0.1);
const onEnd = (reset) => {
Vibration.vibrate(PATTERN);
setIsStarted(false);
setProgress(1);
reset();
onTimerEnd(focusSubject);
};
return (
<View style={styles.container}>
<View style={styles.countdown}>
<Countdown
minutes={minutes}
isPaused={!isStarted}
onProgress={setProgress}
onEnd={onEnd}
/>
<View style={{ paddingTop: spacing.xxl }}>
<Text style={styles.title}>Focusing on:</Text>
<Text style={styles.task}>{focusSubject}</Text>
</View>
</View>
<View style={{ paddingTop: spacing.sm }}>
<ProgressBar
progress={progress}
color={colors.progressBar}
style={{ height: spacing.sm }}
/>
</View>
<View style={styles.timingWrapper}>
<Timing onChangeTime = {setMinutes} />
</View>
<View style={styles.buttonWrapper}>
{!isStarted ? (
<RoundedButton title="start" onPress={() => setIsStarted(true)} />
) : (
<RoundedButton title="pause" onPress={() => setIsStarted(false)} />
)}
</View>
<View style={styles.clearSubjectWrapper}>
<RoundedButton size={50} title="-" onPress={clearSubject} />
</View>
</View>
);
};
//Countdown.js
const minutesToMillis = (min) => min * 1000 * 60;
const formatTime = (time) => (time < 10 ? `0${time}` : time);
export const Countdown = ({ minutes = 0.1, isPaused, onProgress, onEnd }) => {
const interval = React.useRef(null);
const [millis, setMillis] = useState(null);
const reset = () => setMillis(minutesToMillis(minutes))
const countDown = () => {
setMillis((time) => {
if (time === 0) {
clearInterval(interval.current);
onEnd(reset);
return time;
}
const timeLeft = time - 1000;
return timeLeft;
});
};
useEffect(() => {
setMillis(minutesToMillis(minutes));
}, [minutes]);
useEffect(() => {
onProgress(millis / minutesToMillis(minutes));
}, [millis]);
// useEffect(() => {
// onIncrement(millis / minutesToMillis(minutes) 1000);
// },[millis]);
useEffect(() => {
if (isPaused) {
if (interval.current) clearInterval(interval.current);
return;
}
interval.current = setInterval(countDown, 1000);
return () => clearInterval(interval.current);
}, [isPaused]);
const minute = Math.floor(millis / 1000 / 60) % (60);
const seconds = Math.floor(millis / 1000) % (60);
return (
<Text style={styles.text}>
{formatTime(minute)}:{formatTime(seconds)}
</Text>
);
};
CodePudding user response:
Change your Timing
likewise
export const Timing = ({ onChangeTime }) => {
return (
<>
<View style={styles.timingButton}>
<RoundedButton size={75} title=" " onPress={() => onChangeTime(prev => prev 1)} />
</View>
<View style={styles.timingButton}>
<RoundedButton size={75} title="15" onPress={() => onChangeTime(15)} />
</View>
<View style={styles.timingButton}>
<RoundedButton size={75} title="-" onPress={() => onChangeTime( prev => prev - 1)} />
</View>
</>
);
};