This is an Example of React Native Timer and Stopwatch. A Stopwatch is a special watch which can be used to count the time. In stopwatch time starts from zero and run until we stop it but Timer is the exact opposite of it. In the Timer, we decide the time to count and it counts towards zero.
To make a Stopwatch and Timer we have a very good library called react-native-stopwatch-timer
. It will provides Stopwatch
and Timer
component which is very easy to integrate. You can see the example below.
Stopwatch
1 2 3 4 5 6 7 8 9 | <Stopwatch laps msecs start={this.state.isStopwatchStart} //To start reset={this.state.resetStopwatch} //To reset options={options} //options for the styling getTime={this.getFormattedTime} /> |
Timer
1 2 3 4 5 6 7 8 9 10 11 12 13 | <Timer totalDuration={this.state.timerDuration} msecs //Time Duration start={this.state.isTimerStart} //To start reset={this.state.resetTimer} //To reset options={options} //options for the styling handleFinish={handleTimerComplete} //can call a function On finish of the time getTime={this.getFormattedTime} /> |
In this example, we will make a Stopwatch and Timer. The timer will show the Count Down from the given Millisecond. So Let’s get started.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install thereact-native-cli
command line utility. Open the terminal and go to the workspace and runInstallation of Dependency
To use Stopwatch
and Timer
you need to install react-native-stopwatch-timer
package. To install this
Open the terminal and jump into your project using.
1 | cd ProjectName |
run the following command.
1 | npm install react-native-stopwatch-timer --save |
This command will copy all the dependencies into your node_module directory, You can find the directory in node_module
directory named react-native-stopwatch-timer
.
–save is optional, it is just to update the react-native-app-intro-slider dependancy in your package.json file.
Dependency Version for this Example used is
1 | "react-native-stopwatch-timer": "0.0.20" |
Now Open App.js in any code editor and the Replace the code with the following code.
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | /*This is an Example of Timer/Stopwatch in React Native */ import React, { Component } from 'react'; //import React in our project import { StyleSheet,Text,View, TouchableHighlight } from 'react-native'; //import all the required components import { Stopwatch, Timer } from 'react-native-stopwatch-timer'; //importing library to use Stopwatch and Timer export default class TestApp extends Component { constructor(props) { super(props); this.state = { isTimerStart: false, isStopwatchStart: false, timerDuration: 90000, resetTimer: false, resetStopwatch: false, }; this.startStopTimer = this.startStopTimer.bind(this); this.resetTimer = this.resetTimer.bind(this); this.startStopStopWatch = this.startStopStopWatch.bind(this); this.resetStopwatch = this.resetStopwatch.bind(this); } startStopTimer() { this.setState({isTimerStart: !this.state.isTimerStart, resetTimer: false}); } resetTimer() { this.setState({isTimerStart: false, resetTimer: true}); } startStopStopWatch() { this.setState({isStopwatchStart: !this.state.isStopwatchStart, resetStopwatch: false}); } resetStopwatch() { this.setState({isStopwatchStart: false, resetStopwatch: true}); } getFormattedTime(time) { this.currentTime = time; } render() { return ( <View style={{flex:1,justifyContent:'center',alignItems:'center'}}> <View style={{flex:1,marginTop:32, alignItems:'center', justifyContent:'center'}}> <Stopwatch laps msecs start={this.state.isStopwatchStart} //To start reset={this.state.resetStopwatch} //To reset options={options} //options for the styling getTime={this.getFormattedTime} /> <TouchableHighlight onPress={this.startStopStopWatch}> <Text style={{fontSize: 20, marginTop:10}}> {!this.state.isStopwatchStart ? "START" : "STOP"} </Text> </TouchableHighlight> <TouchableHighlight onPress={this.resetStopwatch}> <Text style={{fontSize: 20, marginTop:10}}>RESET</Text> </TouchableHighlight> </View> <View style={{flex:1,marginTop:32, alignItems:'center', justifyContent:'center'}}> <Timer totalDuration={this.state.timerDuration} msecs //Time Duration start={this.state.isTimerStart} //To start reset={this.state.resetTimer} //To reset options={options} //options for the styling handleFinish={handleTimerComplete} //can call a function On finish of the time getTime={this.getFormattedTime} /> <TouchableHighlight onPress={this.startStopTimer}> <Text style={{fontSize: 20, marginTop:10}}> {!this.state.isTimerStart ? "START" : "STOP"} </Text> </TouchableHighlight> <TouchableHighlight onPress={this.resetTimer}> <Text style={{fontSize: 20, marginTop:10}}>RESET</Text> </TouchableHighlight> </View> </View> ); } } const handleTimerComplete = () => alert("Custom Completion Function"); const options = { container: { backgroundColor: '#FF0000', padding: 5, borderRadius: 5, width: 200, alignItems:'center', }, text: { fontSize: 25, color: '#FFF', marginLeft: 7, } }; |
To Run the React Native App
Open the terminal again and jump into your project using.That was the React Native Timer and Stopwatch. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
How useful was this post?
Click on a star to rate us!
Average rating / 5. Vote count:
We are sorry that this post was not useful for you!
Let us improve this post!
Thanks for your feedback!
how to get the stopwatch current time ??
You can see getFormattedTime method which is called when the stopwatch time changes.
getFormattedTime(time) {
this.currentTime = time;
}
You can make a global variable and define the value into it and whenever you want to know the current time you can access it from that variable.