Correctly clear the interval for resource use checking

This commit is contained in:
Dane Everitt 2020-04-10 12:49:09 -07:00
parent d3a06e1ca8
commit 76300209f1
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53

View file

@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useRef, useState } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faServer } from '@fortawesome/free-solid-svg-icons/faServer'; import { faServer } from '@fortawesome/free-solid-svg-icons/faServer';
import { faEthernet } from '@fortawesome/free-solid-svg-icons/faEthernet'; import { faEthernet } from '@fortawesome/free-solid-svg-icons/faEthernet';
@ -21,18 +21,19 @@ const isAlarmState = (current: number, limit: number): boolean => {
}; };
export default ({ server, className }: { server: Server; className: string | undefined }) => { export default ({ server, className }: { server: Server; className: string | undefined }) => {
const interval = useRef<number>(null);
const [ stats, setStats ] = useState<ServerStats | null>(null); const [ stats, setStats ] = useState<ServerStats | null>(null);
const getStats = () => getServerResourceUsage(server.uuid).then(data => setStats(data)); const getStats = () => getServerResourceUsage(server.uuid).then(data => setStats(data));
useEffect(() => { useEffect(() => {
let interval: any = null;
getStats().then(() => { getStats().then(() => {
interval = setInterval(() => getStats(), 20000); // @ts-ignore
interval.current = setInterval(() => getStats(), 20000);
}); });
return () => { return () => {
interval && clearInterval(interval); interval.current && clearInterval(interval.current);
}; };
}, []); }, []);