@@ -3,27 +3,22 @@ import React, {
33 useCallback ,
44 useEffect ,
55 useImperativeHandle ,
6+ useRef ,
67 useState ,
78} from 'react' ;
89import { View , Text } from 'react-native' ;
910import { styles } from './styles' ;
1011import type { CountDownProps } from './model' ;
1112import BackgroundTimer from 'react-native-background-timer' ;
1213
13- const defaulProps = {
14+ const defaultProps = {
1415 style : { } ,
1516 textStyle : { } ,
1617 onTimes : ( _seconds : number ) => { } ,
1718 onPause : ( _seconds : number ) => { } ,
1819 onEnd : ( _seconds : number ) => { } ,
1920} ;
2021
21- let interval : any = null ;
22- let hours = 0 ;
23- let minute = 0 ;
24- let seconds = 0 ;
25- let currentSeconds = 0 ;
26-
2722const CountdownComponent = React . forwardRef < any , CountDownProps > (
2823 ( props , ref ) => {
2924 const {
@@ -37,6 +32,13 @@ const CountdownComponent = React.forwardRef<any, CountDownProps>(
3732 onTimes,
3833 onPause,
3934 } = props ;
35+
36+ const interval = useRef ( 0 ) ;
37+ const hours = useRef ( 0 ) ;
38+ const minute = useRef ( 0 ) ;
39+ const seconds = useRef ( 0 ) ;
40+ const currentSeconds = useRef ( 0 ) ;
41+
4042 const [ key , setKey ] = useState ( Math . random ( ) ) ;
4143
4244 useImperativeHandle ( ref , ( ) => {
@@ -45,31 +47,31 @@ const CountdownComponent = React.forwardRef<any, CountDownProps>(
4547
4648 const init = useCallback ( ( ) => {
4749 if ( initialSeconds ) {
48- currentSeconds = initialSeconds ;
49- hours = ~ ~ ( currentSeconds / 3600 ) ;
50- minute = ~ ~ ( ( currentSeconds % 3600 ) / 60 ) ;
51- seconds = ~ ~ currentSeconds % 60 ;
50+ currentSeconds . current = initialSeconds ;
51+ hours . current = ~ ~ ( currentSeconds . current / 3600 ) ;
52+ minute . current = ~ ~ ( ( currentSeconds . current % 3600 ) / 60 ) ;
53+ seconds . current = ~ ~ currentSeconds . current % 60 ;
5254 }
5355 clear ( ) ;
5456 setKey ( Math . random ( ) ) ;
5557 } , [ initialSeconds ] ) ;
5658
5759 const pause = useCallback ( ( ) => {
5860 if ( onPause ) {
59- onPause ( currentSeconds ) ;
61+ onPause ( currentSeconds . current ) ;
6062 }
6163 clear ( ) ;
6264 } , [ onPause ] ) ;
6365
6466 const resume = ( ) => {
65- if ( ! interval ) {
67+ if ( ! interval . current ) {
6668 timer ( ) ;
6769 }
6870 } ;
6971
7072 const stop = ( ) => {
7173 if ( onEnd ) {
72- onEnd ( currentSeconds ) ;
74+ onEnd ( currentSeconds . current ) ;
7375 }
7476
7577 init ( ) ;
@@ -88,20 +90,20 @@ const CountdownComponent = React.forwardRef<any, CountDownProps>(
8890 } , [ init , initialSeconds ] ) ;
8991
9092 const timer = useCallback ( ( ) => {
91- interval = BackgroundTimer . setInterval ( ( ) => {
92- if ( currentSeconds > 0 ) {
93- currentSeconds = currentSeconds - 1 ;
94- hours = ~ ~ ( currentSeconds / 3600 ) ;
95- minute = ~ ~ ( ( currentSeconds % 3600 ) / 60 ) ;
96- seconds = ~ ~ currentSeconds % 60 ;
93+ interval . current = BackgroundTimer . setInterval ( ( ) => {
94+ if ( currentSeconds . current > 0 ) {
95+ currentSeconds . current = currentSeconds . current - 1 ;
96+ hours . current = ~ ~ ( currentSeconds . current / 3600 ) ;
97+ minute . current = ~ ~ ( ( currentSeconds . current % 3600 ) / 60 ) ;
98+ seconds . current = ~ ~ currentSeconds . current % 60 ;
9799
98100 if ( onTimes ) {
99- onTimes ( currentSeconds ) ;
101+ onTimes ( currentSeconds . current ) ;
100102 }
101103 }
102- if ( currentSeconds <= 0 ) {
104+ if ( currentSeconds . current <= 0 ) {
103105 if ( onEnd ) {
104- onEnd ( currentSeconds ) ;
106+ onEnd ( currentSeconds . current ) ;
105107 }
106108 clear ( ) ;
107109 }
@@ -112,7 +114,7 @@ const CountdownComponent = React.forwardRef<any, CountDownProps>(
112114 const start = useCallback ( ( ) => {
113115 init ( ) ;
114116
115- if ( ! interval ) {
117+ if ( ! interval . current ) {
116118 timer ( ) ;
117119 }
118120 } , [ init , timer ] ) ;
@@ -124,9 +126,9 @@ const CountdownComponent = React.forwardRef<any, CountDownProps>(
124126 } , [ autoStart , initialSeconds , start ] ) ;
125127
126128 const clear = ( ) => {
127- if ( interval ) {
128- BackgroundTimer . clearInterval ( interval ) ;
129- interval = null ;
129+ if ( interval . current ) {
130+ BackgroundTimer . clearInterval ( interval . current ) ;
131+ interval . current = 0 ;
130132 }
131133 } ;
132134
@@ -142,34 +144,36 @@ const CountdownComponent = React.forwardRef<any, CountDownProps>(
142144
143145 const renderTimer = ( ) => {
144146 if ( formatTime === 'hh:mm:ss' ) {
145- if ( hours > 0 ) {
147+ if ( hours . current > 0 ) {
146148 return (
147- < Text style = { [ styles . text , textStyle , font ( ) ] } > { `${ hours } :${
148- minute . toString ( ) . length === 1 ? '0' : ''
149- } ${ minute } :${
150- seconds . toString ( ) . length === 1 ? '0' : ''
151- } ${ seconds } `} </ Text >
149+ < Text style = { [ styles . text , textStyle , font ( ) ] } > { `${ hours . current } :${
150+ minute . current . toString ( ) . length === 1 ? '0' : ''
151+ } ${ minute . current } :${
152+ seconds . current . toString ( ) . length === 1 ? '0' : ''
153+ } ${ seconds . current } `} </ Text >
152154 ) ;
153155 } else {
154- if ( minute > 0 ) {
156+ if ( minute . current > 0 ) {
155157 return (
156- < Text style = { [ styles . text , textStyle , font ( ) ] } > { `${ minute } :${
157- seconds . toString ( ) . length === 1 ? '0' : ''
158- } ${ seconds } `} </ Text >
158+ < Text style = { [ styles . text , textStyle , font ( ) ] } > { `${
159+ minute . current
160+ } :${ seconds . current . toString ( ) . length === 1 ? '0' : '' } ${
161+ seconds . current
162+ } `} </ Text >
159163 ) ;
160164 } else {
161165 return (
162166 < Text
163167 style = { [ styles . text , textStyle , font ( ) ] }
164- > { `${ seconds } ` } </ Text >
168+ > { `${ seconds . current } ` } </ Text >
165169 ) ;
166170 }
167171 }
168172 } else {
169173 return (
170174 < Text
171175 style = { [ styles . text , textStyle , font ( ) ] }
172- > { `${ currentSeconds } ` } </ Text >
176+ > { `${ currentSeconds . current } ` } </ Text >
173177 ) ;
174178 }
175179 } ;
@@ -182,6 +186,6 @@ const CountdownComponent = React.forwardRef<any, CountDownProps>(
182186 }
183187) ;
184188
185- CountdownComponent . defaultProps = defaulProps ;
189+ CountdownComponent . defaultProps = defaultProps ;
186190
187191export default CountdownComponent ;
0 commit comments