Passed
Push — master ( 4fffb9...a79de5 )
by Guangyu
08:15 queued 12s
created

useInterval.js ➔ tick   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
1
import React, { useState, useEffect, useRef } from 'react';
2
3
const useInterval = (callback, delay) => {
4
  const savedCallback = useRef();
5
6
  // Remember the latest callback.
7
  useEffect(() => {
8
    savedCallback.current = callback;
9
  });
10
11
  // Set up the interval.
12
  useEffect(() => {
13
    function tick() {
14
      savedCallback.current();
15
    }
16
    if (delay !== null) {
17
      let id = setInterval(tick, delay);
18
      return () => clearInterval(id);
19
    }
20
  }, [delay]);
21
};
22
export default useInterval;
23