Skip to content

Instantly share code, notes, and snippets.

View Bviveksingh's full-sized avatar
🙂
Focusing

Vivek Singh Bisht Bviveksingh

🙂
Focusing
View GitHub Profile
@Bviveksingh
Bviveksingh / timer.tsx
Created September 15, 2022 18:47
useEffect for readableFormat
useEffect(() => {
setReadableFormat(getReadableFormat);
},[counterInSeconds]);
@Bviveksingh
Bviveksingh / timer.tsx
Created September 15, 2022 18:46
useEffect setting counterInSeconds
useEffect(() => {
setCounterInSeconds(timeInSeconds);
},[timeInSeconds]);
@Bviveksingh
Bviveksingh / timer.tsx
Created September 15, 2022 18:44
useEffect of timer
useEffect(() => {
if(counterInSeconds as number > 0 && startTimer){
setTimeout(() => {
setCounterInSeconds(prevCount => prevCount as number - 1);
setReadableFormat(getReadableFormat);
}, 1000);
}
if(counterInSeconds === 0){
endTimerFunc();
setCounterInSeconds(timeInSeconds);
@Bviveksingh
Bviveksingh / timer.tsx
Created September 15, 2022 18:41
getReadableFormat
const getReadableFormat = useMemo(() => {
let seconds : string | number = counterInSeconds as number % 60;
let minutes : string | number = Math.floor(counterInSeconds as number / 60);
minutes = minutes.toString().length === 1 ? "0" + minutes : minutes;
seconds = seconds.toString().length === 1 ? "0" + seconds : seconds;
return `${minutes} : ${seconds}`;
}, [counterInSeconds]);
@Bviveksingh
Bviveksingh / timer.tsx
Last active September 15, 2022 18:50
definition of Timer component src/components/Timer/index.tsx
import React, { FC, useEffect, useMemo, useState } from 'react'
interface TimerProps {
startTimer: boolean;
endTimerFunc: () => void;
timeInSeconds: number;
}
const Timer : FC<TimerProps> = ({
startTimer,
@Bviveksingh
Bviveksingh / index.tsx
Created September 15, 2022 18:32
conditional rendering of displayResult
{endTimer && displayResult()}
@Bviveksingh
Bviveksingh / index.tsx
Created September 15, 2022 18:31
definition of inputVal
const [inputVal, setInputVal] = useState<string>('');
@Bviveksingh
Bviveksingh / index.tsx
Created September 15, 2022 18:30
conditional rendering of reset button
{inputVal.length > 0 && <button>Reset</button>}
@Bviveksingh
Bviveksingh / index.tsx
Created September 15, 2022 18:29
endTimer and eligibleToStart booleans definition
const [endTimer, setEndTimer] = useState<boolean>(false);
const [eligibleToStart, setElegibleToStart] = useState<boolean>(false);
@Bviveksingh
Bviveksingh / index.tsx
Created September 15, 2022 18:28
conditional rendering of input box
{!endTimer && eligibleToStart && <InputBox/>}