react-use-count-down icon indicating copy to clipboard operation
react-use-count-down copied to clipboard

initial state should not be '0'

Open xwuji opened this issue 3 years ago • 6 comments

const [timeLeft, setTimeLeft] = React.useState(0); Here,initial state should not be '0',use the argus 'timeToCount' is more better. cause ,'0' is the mark of the beginning and the end,Can cause initialization rendering problems

xwuji avatar Apr 25 '21 04:04 xwuji

Hey @joacycode I'm not really getting the issue. If you pass 0 as an initial value, timeLeft's going to be 0 as well. Am I missing something? Can you elaborate?

alexkhismatulin avatar Apr 25 '21 10:04 alexkhismatulin

Hey @alexkhismatulin, I think he is referring to this line here.

Because it defaults to 0, doing any sort of const expired = timeLeft === 0 checks leads to expired being true on the first render (as the state is defaulted to 0).

I think this also plays into this issue

keezeden avatar May 18 '21 02:05 keezeden

What is the purpose of the counter if I can't catch the moment it expires? I think this is the fundamental feature that is missing

berkerdemirer avatar Nov 29 '21 16:11 berkerdemirer

Maybe it would be good if the hook has internal flag, something like const [timeLeft, { flag }] = useCountDown(initialTime, interval)

and we could do like flag === COUNTDOWN_NOT_STARTED_YET

AarRidho avatar Dec 24 '21 15:12 AarRidho

Any updates on this ? . I can't really use the library since, There is no way to determine whether the time is expired or it's very beginning.

azakaryan avatar Mar 15 '22 08:03 azakaryan

While less-than-ideal, there is a workaround available:

const [started, setStarted] = useState(false);

useEffect(() => {
  start();
  setStarted(true);
}, []);

useEffect(() => {
  if (started && timeLeft === 0) props.callback();
}, [timeLeft]);

We create a state variable and set its value after we initialise timer. Then, in whatever place we need to check time left, we check if timer has started and then check timeLeft variable. Again, it's a workaround until author can fix the issue permanently.

alexkuc avatar May 21 '22 16:05 alexkuc