Add useUnstrict.js
This commit is contained in:
commit
24618f6907
1 changed files with 42 additions and 0 deletions
42
useUnstrict.js
Normal file
42
useUnstrict.js
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
import React, { useEffect, useRef } from 'react'
|
||||||
|
|
||||||
|
const useUnstrictEffect = function (og_func, og_deps) {
|
||||||
|
|
||||||
|
const ref_obj = {}
|
||||||
|
|
||||||
|
const ref_teardown = useRef()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
|
||||||
|
const id_timeout = setTimeout(() => {
|
||||||
|
|
||||||
|
// do the original thing //
|
||||||
|
const func_teardown = og_func()
|
||||||
|
|
||||||
|
ref_teardown.current = () => {
|
||||||
|
func_teardown()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
}, 1)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (ref_teardown.current) {
|
||||||
|
ref_teardown.current()
|
||||||
|
ref_teardown.current = undefined
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// immediate Unmount = STOP og_func from ever running
|
||||||
|
clearTimeout(id_timeout)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}, [ ...og_deps ])
|
||||||
|
|
||||||
|
return ref_obj
|
||||||
|
|
||||||
|
}
|
||||||
|
export { useUnstrictEffect }
|
Reference in a new issue