use-interval
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -1,2 +0,2 @@ | ||
export declare function useInterval(callback: () => void, delay: number): void; | ||
export declare function useInterval(callback: () => void, delay: number | null, immediate?: boolean): void; | ||
export default useInterval; |
import { useRef, useEffect } from 'react'; | ||
function useInterval(callback, delay) { | ||
var savedCallback = useRef(function () { }); | ||
/* istanbul ignore next */ | ||
/** keep typescript happy */ | ||
var noop = function () { }; | ||
function useInterval(callback, delay, immediate) { | ||
var savedCallback = useRef(noop); | ||
// Remember the latest callback. | ||
useEffect(function () { | ||
savedCallback.current = callback; | ||
}, [callback]); | ||
}); | ||
// Execute callback if immediate is set. | ||
useEffect(function () { | ||
if (!immediate) | ||
return; | ||
if (delay === null) | ||
return; | ||
savedCallback.current(); | ||
}, [immediate]); | ||
// Set up the interval. | ||
useEffect(function () { | ||
function tick() { | ||
savedCallback.current(); | ||
} | ||
if (delay !== null) { | ||
var id_1 = setInterval(tick, delay); | ||
return function () { return clearInterval(id_1); }; | ||
} | ||
else { | ||
return function () { }; | ||
} | ||
if (delay === null) | ||
return undefined; | ||
var tick = function () { return savedCallback.current(); }; | ||
var id = setInterval(tick, delay); | ||
return function () { return clearInterval(id); }; | ||
}, [delay]); | ||
@@ -22,0 +28,0 @@ } |
@@ -7,20 +7,26 @@ 'use strict'; | ||
function useInterval(callback, delay) { | ||
var savedCallback = react.useRef(function () { }); | ||
/* istanbul ignore next */ | ||
/** keep typescript happy */ | ||
var noop = function () { }; | ||
function useInterval(callback, delay, immediate) { | ||
var savedCallback = react.useRef(noop); | ||
// Remember the latest callback. | ||
react.useEffect(function () { | ||
savedCallback.current = callback; | ||
}, [callback]); | ||
}); | ||
// Execute callback if immediate is set. | ||
react.useEffect(function () { | ||
if (!immediate) | ||
return; | ||
if (delay === null) | ||
return; | ||
savedCallback.current(); | ||
}, [immediate]); | ||
// Set up the interval. | ||
react.useEffect(function () { | ||
function tick() { | ||
savedCallback.current(); | ||
} | ||
if (delay !== null) { | ||
var id_1 = setInterval(tick, delay); | ||
return function () { return clearInterval(id_1); }; | ||
} | ||
else { | ||
return function () { }; | ||
} | ||
if (delay === null) | ||
return undefined; | ||
var tick = function () { return savedCallback.current(); }; | ||
var id = setInterval(tick, delay); | ||
return function () { return clearInterval(id); }; | ||
}, [delay]); | ||
@@ -27,0 +33,0 @@ } |
{ | ||
"name": "use-interval", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "React hook for setting an interval as posted on overreacted.io", | ||
@@ -16,4 +16,4 @@ "author": "Hermanya", | ||
"scripts": { | ||
"test": "cross-env CI=1 react-scripts test --env=jsdom", | ||
"test:watch": "react-scripts test --env=jsdom", | ||
"test": "cross-env CI=1 react-scripts test --env=jsdom --coverage", | ||
"test:watch": "react-scripts test --env=jsdom --coverage --watch", | ||
"build": "rollup -c", | ||
@@ -25,16 +25,19 @@ "start": "rollup -c -w", | ||
}, | ||
"dependencies": {}, | ||
"peerDependencies": { | ||
"react": "^16.8.0-alpha.1" | ||
"react": "^16.8.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.2.2", | ||
"@babel/core": "^7.3.3", | ||
"@babel/runtime": "^7.3.1", | ||
"@types/jest": "^23.3.13", | ||
"@types/react": "^16.7.22", | ||
"@types/react-dom": "^16.8.2", | ||
"@types/react": "^16.8.4", | ||
"babel-preset-react-app": "^7.0.0", | ||
"cross-env": "^5.2.0", | ||
"gh-pages": "^2.0.1", | ||
"react": "^16.8.0-alpha.1", | ||
"react-scripts": "^2.1.3", | ||
"rollup": "^1.1.2", | ||
"prettier": "1.16.4", | ||
"react": "^16.8.2", | ||
"react-dom": "^16.8.2", | ||
"react-scripts": "^2.1.5", | ||
"rollup": "^1.2.2", | ||
"rollup-plugin-babel": "^4.3.2", | ||
@@ -46,3 +49,3 @@ "rollup-plugin-commonjs": "^9.2.0", | ||
"rollup-plugin-url": "^2.1.0", | ||
"typescript": "^3.2.4" | ||
"typescript": "^3.3.3" | ||
}, | ||
@@ -49,0 +52,0 @@ "files": [ |
@@ -34,2 +34,11 @@ # use-interval | ||
```tsx | ||
// TypeScript Declaration | ||
useInterval( | ||
callback: () => void, | ||
delay: number, | ||
immediate?: boolean /* called when mounted if true */ | ||
) | ||
``` | ||
## License | ||
@@ -36,0 +45,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
9568
10
72
50
20