In one of my mobile apps, I needed to add a count down timer – which counts back from a specified time to zero time. I was preparing to create the same from the scratch until I saw this new third-party library named react native countdown timer.
Even though the react native library is relatively new, I choose it instead of reinventing the wheel. The timer component is easy to implement as well as customize. ‘
All you have to do is
npm install –save react-native-timer-countdown
Following is the example code to implement countdown timer in react native. For more details, you can check the official page.
import React from "react";
import { StyleSheet, View } from "react-native";
import TimerCountdown from "react-native-timer-countdown";
const App = () => (
<View style={styles.container}>
<TimerCountdown
initialMilliseconds={1000 * 60}
onTick={(milliseconds) => console.log("tick", milliseconds)}
onExpire={() => console.log("complete")}
formatMilliseconds={(milliseconds) => {
const remainingSec = Math.round(milliseconds / 1000);
const seconds = parseInt((remainingSec % 60).toString(), 10);
const minutes = parseInt(((remainingSec / 60) % 60).toString(), 10);
const hours = parseInt((remainingSec / 3600).toString(), 10);
const s = seconds < 10 ? '0' + seconds : seconds;
const m = minutes < 10 ? '0' + minutes : minutes;
let h = hours < 10 ? '0' + hours : hours;
h = h === '00' ? '' : h + ':';
return h + m + ':' + s;
}}
allowFontScaling={true}
style={{ fontSize: 20 }}
/>
</View>
);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
}
});
export default App;