Timer in Spfx (React)

Introduction

 
In this article, we learn how to implement a timer in SPFx. The 'useTimer' is a react plugin, written using react hooks, that allows us to do timer functions in our component.
 
Steps 
 
Open a command prompt and create a directory for the SPFx solution.
 
md spfx-ReactTimer
 
Navigate to the above-created directory.
 
cd spfx-ReactTimer
 
Run the Yeoman SharePoint Generator to create the solution.
 
yo @microsoft/sharepoint
 
Solution Name
 
Hit Enter to have a default name (spfx-ReactTimer in this case), or type in any other name for your solution.
Selected choice - Hit Enter
 
Target for the component
 
Here, we can select the target environment where we are planning to deploy the client web part; i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest).
 
Place of files
 
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - same folder.
 
Deployment option
 
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly).
 
Permissions to access web APIs
 
Choose if the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
 
Type of client-side component to create
 
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
 
Web part name
 
Hit Enter to select the default name or type in any other name.
Selected choice - ReactTimer
 
Web part description
 
Hit Enter to select the default description or type in any other value.
 
Framework to use
 
Select any JavaScript framework to develop the component. The available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - React
 
The Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command,
 
npm shrinkwrap
 
In the command prompt, type the below command to open the solution in the code editor of your choice.
  1. npm i use-timer//for timer function  
  2. npm i bootstrap//for bootsrap css its optional 
Necessary imports
  1. import { useTimer } from "use-timer";  
  2. import "bootstrap/dist/css/bootstrap.min.css";//optional  
Full Code
 
In ReactTimer.tsx
  1. import * as React from 'react';  
  2. import { IReactTimerProps } from './IReactTimerProps';  
  3. import "bootstrap/dist/css/bootstrap.min.css";  
  4. import {BasicTimer,DecrementalTimer,TimerWithEndTime} from './mytimer';  
  5. export default class ReactTimer extends React.Component<IReactTimerProps, {}> {  
  6.   public render(): React.ReactElement<IReactTimerProps> {  
  7.     return (  
  8.       <div className="container">  
  9.    <BasicTimer />  
  10.       <DecrementalTimer />  
  11.       <TimerWithEndTime />  
  12.       </div>  
  13.     );  
  14.   }  

  In mytimer.tsx,
  1. import * as React from 'react';  
  2. import { useTimer } from "use-timer";  
  3. import "bootstrap/dist/css/bootstrap.min.css";  
  4. import RunningButton from "./RunningButton";  
  5. import "./mystyle.css";  
  6. export const BasicTimer = () => {  
  7.     const { time, start, pause, reset, isRunning } = useTimer();  
  8.     
  9.     return (  
  10.       <div className="card">  
  11.         <h5 className="card-header">Basic timer</h5>  
  12.         <div className="card-body">  
  13.           {isRunning ? (  
  14.             <RunningButton />  
  15.           ) : (  
  16.             <button className="btn btn-primary" onClick={start}>  
  17.               Start  
  18.             </button>  
  19.           )}  
  20.           <button className="btn btn-primary" onClick={pause}>  
  21.             Pause  
  22.           </button>  
  23.           <button className="btn btn-primary" onClick={reset}>  
  24.             Reset  
  25.           </button>  
  26.         </div>  
  27.         <div className="card-footer">  
  28.           Elapsed time: <strong>{time}</strong>  
  29.         </div>  
  30.       </div>  
  31.     );  
  32.   };  
  33.     
  34.   export const DecrementalTimer = () => {  
  35.     const { time, start, pause, reset, isRunning } = useTimer({  
  36.       initialTime: 100,  
  37.       timerType: "DECREMENTAL"  
  38.     });  
  39.     
  40.     return (  
  41.       <div className="card">  
  42.         <h5 className="card-header">Decremental timer</h5>  
  43.         <div className="card-body">  
  44.           {isRunning ? (  
  45.             <RunningButton />  
  46.           ) : (  
  47.             <button className="btn btn-primary" onClick={start}>  
  48.               Start  
  49.             </button>  
  50.           )}  
  51.           <button className="btn btn-primary" onClick={pause}>  
  52.             Pause  
  53.           </button>  
  54.           <button className="btn btn-primary" onClick={reset}>  
  55.             Reset  
  56.           </button>  
  57.         </div>  
  58.         <div className="card-footer">  
  59.           Remaining time: <strong>{time}</strong>  
  60.         </div>  
  61.       </div>  
  62.     );  
  63.   };  
  64.     
  65.   export const TimerWithEndTime = () => {  
  66.     const endTime = 5;  
  67.     const { time, start, pause, reset, isRunning } = useTimer({  
  68.       endTime  
  69.     });  
  70.     
  71.     return (  
  72.       <div className="card">  
  73.         <h5 className="card-header">Timer with end time</h5>  
  74.         <div className="card-body">  
  75.           {isRunning ? (  
  76.             <RunningButton />  
  77.           ) : (  
  78.             <button className="btn btn-primary" onClick={start}>  
  79.               Start  
  80.             </button>  
  81.           )}  
  82.           <button className="btn btn-primary" onClick={pause}>  
  83.             Pause  
  84.           </button>  
  85.           <button className="btn btn-primary" onClick={reset}>  
  86.             Reset  
  87.           </button>  
  88.         </div>  
  89.         <div className="card-footer">  
  90.           {time === endTime ? (  
  91.             <span>Finished!</span>  
  92.           ) : (  
  93.             <span>  
  94.               Ellapsed time: <strong>{time}</strong>  
  95.             </span>  
  96.           )}  
  97.         </div>  
  98.       </div>  
  99.     );  
  100.   };  
  101.    
In mystyle.css:
  1. h1, p {  
  2.     font-family: Lato;  
  3.   }  
  4.     
  5.   button{  
  6.     margin-right: 5px;  
  7.   }  
  8.     
  9.   .card{  
  10.     margin-bottom: 30px;  
  11.   }  
In RunningButton.tsx:
  1. import * as React from 'react';  
  2.   
  3. const RunningButton = () => <button className="btn btn-primary" disabled style={{ cursor: 'default'}}>Running...</button>;  
  4.   
  5. export default RunningButton; 
 Properties
 
 Property  Description
 endTime  the value for which timer stops
 initialTime  the starting value for the timer
 interval  the interval in milliseconds
 step  the value to add to each increment/decrement
 timerType  the choice between a value that increases ("INCREMENTAL") or decreases ("DECREMENTAL")
 
Expected Output
 
 
 

Conclusion

 
In this post, we learned how to implement timers in SPFx. I hope this helps someone, happy coding! :)