react-native-countdown-component icon indicating copy to clipboard operation
react-native-countdown-component copied to clipboard

until does not update when state.property is changed - stays one update behind.

Open lazynewt opened this issue 4 years ago • 6 comments

Hi when i am incrementing the until time the countdown UI does not reflect the change. However when i then update it again it reflects the previous value. e.g

state.until is used and set initially as 60 and is displaying 1 minute I then set state.until = 180 yet the countdown still displays 1 minute I then set state.until = 240 and the countdown now shows 2 minutes I then set the state.until to 300 and the countdown shows 3 minutes

It remains 1 UI update behind.

I have tried .forcerefresh

Is there a reason for this behaviour ? (running 2.7.1) Thanks.

lazynewt avatar Jun 07 '21 10:06 lazynewt

Can someone please update/ commit this change# componentDidUpdate(prevProps, prevState) { if (this.props.until !== prevProps.until || this.props.id !== prevProps.id) { this.setState({ lastUntil: 0, until: Math.max(this.props.until, 0) }); } }

Or even advise me on how i can which would be even better ? Thanks.

lazynewt avatar Jun 07 '21 11:06 lazynewt

Also running into this same issue and would love to see it fixed

chrisozgo99 avatar Jul 20 '21 18:07 chrisozgo99

also same problem. + 1

dieptang avatar Mar 23 '22 11:03 dieptang

Maybe you can use props "id". When you update "until" props value on state you also need to update "id" props value as well. just use random string generator for "id" and it's working fine for me.

gredy-devstack avatar Mar 29 '22 11:03 gredy-devstack

you can search pull request, change the original index file to that version, I have made a pull request to solve this problem.

pwliuab avatar Apr 03 '22 04:04 pwliuab

Hi guys,

I was having the same problem and I figured it out. Actually, it's written in the documentation: Props -> id: string: Counter id, to determine whether to reset the counter or not.

You just have to update your component's id every time your until's prop value changes, as such:

import React, { useEffect, useState } from "react"
import { useSelector, useDispatch } from "react-redux"
import CountDown from "react-native-countdown-component"
import { setRest } from "../data/restSlice"

const Timer = () => {
  const dispatch = useDispatch()
  /** rest time from store */
  const restTime = useSelector((state) => (state as any).rest.value)
  /** countdown component id */
  const [countDownId, setCountDownId] = useState(undefined)

  /**
   * Update countdown component id every time
   * rest time changes.
   *
   * Needed to refresh countdown component.
   */
  useEffect(() => {
    // Generate new id based on unix timestamp (string)
    const id = new Date().getTime().toString()
    // Set id to state
    setCountDownId(id)
  }, [restTime])

  return (
    <CountDown
      id={countDownId}
      until={restTime}
      onFinish={() => dispatch(setRest(0))}
      onPress={() => dispatch(setRest(0))}
      size={30}
      timeToShow={["M", "S"]}
    />
  )
}

Hope it helps!

gerardcsaperas avatar Apr 16 '22 11:04 gerardcsaperas