From 24618f6907bc3f967c8a97bd7f2f9c69cf953287 Mon Sep 17 00:00:00 2001 From: dab Date: Wed, 30 Apr 2025 16:13:57 +0000 Subject: [PATCH] Add useUnstrict.js --- useUnstrict.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 useUnstrict.js diff --git a/useUnstrict.js b/useUnstrict.js new file mode 100644 index 0000000..0520af2 --- /dev/null +++ b/useUnstrict.js @@ -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 } \ No newline at end of file