Skip to content

Commit 523f7b1

Browse files
committed
Fix spinner timeout clearing
1 parent e8a7b17 commit 523f7b1

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/components/Spinner.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useEffect, useState} from 'react';
1+
import React, {useEffect, useRef, useState} from 'react';
22
import PropTypes from 'prop-types';
33
import {omit} from 'ramda';
44
import {Spinner as RSSpinner} from 'reactstrap';
@@ -26,13 +26,17 @@ const Spinner = props => {
2626
} = props;
2727

2828
const [showSpinner, setShowSpinner] = useState(false);
29+
const timer = useRef();
2930

3031
useEffect(() => {
3132
if (loading_state) {
33+
if (timer.current) {
34+
clearTimeout(timer.current);
35+
}
3236
if (loading_state.is_loading && !showSpinner) {
3337
setShowSpinner(true);
3438
} else if (!loading_state.is_loading && showSpinner) {
35-
setTimeout(() => setShowSpinner(false), debounce);
39+
timer.current = setTimeout(() => setShowSpinner(false), debounce);
3640
}
3741
}
3842
}, [loading_state]);
@@ -180,8 +184,8 @@ Spinner.propTypes = {
180184
fullscreen: PropTypes.bool,
181185

182186
/**
183-
* When using the spinner as a loading spinner, add a time delay to the
184-
* spinner being removed to prevent flickering.
187+
* When using the spinner as a loading spinner, add a time delay (in ms) to
188+
* the spinner being removed to prevent flickering.
185189
*/
186190
debounce: PropTypes.number
187191
};

0 commit comments

Comments
 (0)