Hello,
I’m currently trying to implement a counterdown using the following two packages:
-
react-native-countdown-component
-
moment
For orientation, I looked at the following example and first copied and pasted most of the code and adjusted the variable “expirydate” and changed “utcOffset” to “+02:00” German time: CountDown timer Example
The problem I have now is that as soon as I run the app via Expo Go on my phone, there is a zero everywhere by default (CountDown is not started). I have to explicitly save the corresponding JavaScript file for it to run. Can anyone here tell me what the problem might be and what I need to do to start the counter immediately after opening my screen? My code is structured as follows:
import CountDown from 'react-native-countdown-component';
import moment from 'moment';
export default function Produktdetail({ route, navigation }) {
const [totalDuration, setTotalDuration] = useState(0);
useEffect(() => {
var date = moment().utcOffset('+01:00').format('YYYY-MM-DD hh:mm:ss');
//Getting the current date-time with required formate and UTC
var expirydate = '2021-07-09 00:00:01'; //You can set your own date-time
//Let suppose we have to show the countdown for above date-time
var diffr = moment.duration(moment(expirydate).diff(moment(date)));
//difference of the expiry date-time given and current date-time
var hours = parseInt(diffr.asHours());
var minutes = parseInt(diffr.minutes());
var seconds = parseInt(diffr.seconds());
var d = hours * 60 * 60 + minutes * 60 + seconds;
//converting in seconds
setTotalDuration(d);
//Settign up the duration of countdown in seconds to re-render
}, []);
return (
<View style={{marginTop: 20}}>
<CountDown
until={totalDuration}
//duration of countdown in seconds
timetoShow={('H', 'M', 'S')}
//on Press call
size={20}
/>
</View>
);
}