React Native Timer Countdown
⏱ A minimal and customizable countdown timer
for React Native (iOS and Android)
Installation
npm install --save react-native-timer-countdown
or
yarn add react-native-timer-countdown
Example
;;; const App = <View style=stylescontainer> <TimerCountdown initialMilliseconds=1000 * 60 onTick= console onExpire= console formatMilliseconds= { const remainingSec = Math; const seconds = ; const minutes = ; const hours = ; 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; ;
Props
Name | Type | Description | Required |
---|---|---|---|
initialMilliseconds | number | The remaining milliseconds for the countdown | Yes |
formatMilliseconds | function (milliseconds: number) => void |
A function that formats the milliseconds | No |
onTick | function (milliseconds: number) => void |
A function to call each tick | No |
onExpire | function () => void |
A function to call when the countdown finishes | No |
allowFontScaling | boolean | A boolean for allowFontScaling. The default is false |
No |
style | object | Custom style to be applied to the Text component | No |
FAQ
Why does this timer restart whenever I click any button?
What's happening
buttons clicked -> state changes -> react rerenders -> timer restarts
How to not to restart the timer component
Provided the state changes only occur in component B, A component will not rerender. As a result, no more unintended timer restarts.
;;; const A = <View style=stylescontainer> <B /> <TimerCountdown initialMilliseconds=1000 * 60 /> </View>;; state = isPressed: false ; { return <View styles= flex: 1 > <Button title=`` onPress= { this; } /> </View> ; } const styles = StyleSheet;
Author
License
MIT