This repository has been archived on 2025-06-06. You can view files and clone it, but cannot push or open issues or pull requests.
react_examples/useUnstrict.js
2025-04-30 16:13:57 +00:00

42 lines
No EOL
704 B
JavaScript

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 }