Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 23 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |