use-throttle
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -18,19 +18,14 @@ 'use strict'; | ||
var _useState3 = (0, _react.useState)(Date.now()), | ||
_useState4 = _slicedToArray(_useState3, 2), | ||
lastRan = _useState4[0], | ||
setLastRan = _useState4[1]; | ||
var lastRan = (0, _react.useRef)(Date.now()); | ||
var handler = (0, _react.useRef)(); | ||
(0, _react.useEffect)(function () { | ||
handler.current = setTimeout(function () { | ||
if (Date.now() - lastRan >= limit) { | ||
var handler = setTimeout(function () { | ||
if (Date.now() - lastRan.current >= limit) { | ||
setThrottledValue(value); | ||
setLastRan(Date.now()); | ||
lastRan.current = Date.now(); | ||
} | ||
}, limit - (Date.now() - lastRan)); | ||
}, limit - (Date.now() - lastRan.current)); | ||
return function () { | ||
clearTimeout(handler.current); | ||
clearTimeout(handler); | ||
}; | ||
@@ -37,0 +32,0 @@ }, [value, limit]); |
{ | ||
"name": "use-throttle", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Throttle hook for react", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,16 +5,15 @@ import { useState, useEffect, useRef } from 'react'; | ||
const [throttledValue, setThrottledValue] = useState(value); | ||
const [lastRan, setLastRan] = useState(Date.now()); | ||
const handler = useRef(); | ||
const lastRan = useRef(Date.now()); | ||
useEffect( | ||
() => { | ||
handler.current = setTimeout(function() { | ||
if (Date.now() - lastRan >= limit) { | ||
const handler = setTimeout(function() { | ||
if (Date.now() - lastRan.current >= limit) { | ||
setThrottledValue(value); | ||
setLastRan(Date.now()); | ||
lastRan.current = Date.now(); | ||
} | ||
}, limit - (Date.now() - lastRan)); | ||
}, limit - (Date.now() - lastRan.current)); | ||
return () => { | ||
clearTimeout(handler.current); | ||
clearTimeout(handler); | ||
}; | ||
@@ -21,0 +20,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92184
47