Unleash react/preact hooks from components, make them available in regular javascript without hooks limitations.
import { useEffect, useState } from 'unhook'
function tick() {
let [count, setCount] = useState(0)
useEffect(() => {
let id = setInterval(() => {
setCount(count => count + 1)
// NOTE: function must be triggered manually
tick()
}, 1000)
return () => clearInterval(id)
}, [])
}
Internally, unhook uses error.stack
, which is non-standard, but well supported by all browsers/engines for a long time.
For production, it is possible to statically compile source to avoid using error.stack
. (pending feature...)
- enhook - turn a function into hooks-enabled function.
- any-hooks - obtain any installed hooks.
- unihooks - collection of universal hooks
HK