react-timing-hooks
Advanced tools
Comparing version 1.4.0 to 1.4.1
@@ -5,2 +5,9 @@ # Changelog | ||
### [1.4.1](https://github.com/EricLambrecht/react-timing-hooks/compare/v1.4.0...v1.4.1) (2020-07-25) | ||
### Bug Fixes | ||
* Fix timeouts not being properly unmounted (if more than 1) ([df2d247](https://github.com/EricLambrecht/react-timing-hooks/commit/df2d2476a5bd899ba8a362cb2ae5636f6d4089ab)), closes [#4](https://github.com/EricLambrecht/react-timing-hooks/issues/4) | ||
## [1.4.0](https://github.com/EricLambrecht/react-timing-hooks/compare/v1.3.2...v1.4.0) (2020-06-12) | ||
@@ -7,0 +14,0 @@ |
import { useRef, useCallback, useEffect, useState } from 'react'; | ||
const useTimeoutEffect = (effect, deps) => { | ||
const timeoutId = useRef(null); | ||
const timeoutIds = useRef([]); | ||
const timeoutFunc = useCallback((handler, timeout) => { | ||
timeoutId.current = setTimeout(handler, timeout); | ||
}, [timeoutId]); | ||
const id = setTimeout(handler, timeout); | ||
timeoutIds.current.push(id); | ||
}, [timeoutIds]); | ||
useEffect(() => { | ||
return effect(timeoutFunc, () => { | ||
if (timeoutId.current) { | ||
clearTimeout(timeoutId.current); | ||
if (timeoutIds.current.length > 0) { | ||
timeoutIds.current.forEach(id => { | ||
clearTimeout(id); | ||
}); | ||
} | ||
@@ -17,7 +20,9 @@ }); | ||
return function onUnmount() { | ||
if (timeoutId.current !== null) { | ||
clearTimeout(timeoutId.current); | ||
if (timeoutIds.current.length > 0) { | ||
timeoutIds.current.forEach(id => { | ||
clearTimeout(id); | ||
}); | ||
} | ||
}; | ||
}, [timeoutId]); | ||
}, [timeoutIds]); | ||
}; | ||
@@ -24,0 +29,0 @@ |
@@ -8,10 +8,13 @@ 'use strict'; | ||
const useTimeoutEffect = (effect, deps) => { | ||
const timeoutId = react.useRef(null); | ||
const timeoutIds = react.useRef([]); | ||
const timeoutFunc = react.useCallback((handler, timeout) => { | ||
timeoutId.current = setTimeout(handler, timeout); | ||
}, [timeoutId]); | ||
const id = setTimeout(handler, timeout); | ||
timeoutIds.current.push(id); | ||
}, [timeoutIds]); | ||
react.useEffect(() => { | ||
return effect(timeoutFunc, () => { | ||
if (timeoutId.current) { | ||
clearTimeout(timeoutId.current); | ||
if (timeoutIds.current.length > 0) { | ||
timeoutIds.current.forEach(id => { | ||
clearTimeout(id); | ||
}); | ||
} | ||
@@ -22,7 +25,9 @@ }); | ||
return function onUnmount() { | ||
if (timeoutId.current !== null) { | ||
clearTimeout(timeoutId.current); | ||
if (timeoutIds.current.length > 0) { | ||
timeoutIds.current.forEach(id => { | ||
clearTimeout(id); | ||
}); | ||
} | ||
}; | ||
}, [timeoutId]); | ||
}, [timeoutIds]); | ||
}; | ||
@@ -29,0 +34,0 @@ |
{ | ||
"name": "react-timing-hooks", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "React hooks for setTimeout, setInterval, requestAnimationFrame, requestIdleCallback", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -54,2 +54,6 @@ [![npm](https://flat.badgen.net/npm/v/react-timing-hooks)](https://www.npmjs.com/package/react-timing-hooks) | ||
``` | ||
## Documentation | ||
[https://ericlambrecht.github.io/react-timing-hooks/](https://ericlambrecht.github.io/react-timing-hooks/) | ||
@@ -115,6 +119,2 @@ ## Why bother? | ||
In this case `react-timing-hooks` automatically took care of cleaning up the timeout for you (if the component is mounted for less than a second for instance). | ||
## Documentation | ||
[https://ericlambrecht.github.io/react-timing-hooks/](https://ericlambrecht.github.io/react-timing-hooks/) | ||
@@ -121,0 +121,0 @@ |
Sorry, the diff of this file is not supported yet
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
31535
403