helix’s hooks seem quite nice 👍 might make use of some of these — are there any that you consider more “alpha/beta” than others @lilactown?
I wrote my first custom hook today and it felt like a lot of things “clicked”, would be curious if I’m doing anything wrong here:
(defn use-interval-counter
"A use-state like hook that provides an incrementing counter for every `ms`
milliseconds that pass. Commonly used for countdowns and timeouts."
[{:keys [ms]}]
(let [[secs-passed set-secs-passed!] (rum/use-state 0)]
(rum/use-effect! (fn []
(let [timer (js/setInterval #(set-secs-passed! inc) ms)]
(fn cleanup-timer []
(js/clearInterval timer))))
[])
[secs-passed set-secs-passed!]))
One minor issues with this is that when calling set-secs-passed
the first firing of the interval may be faster than the provided ms
interval. I think with an additional use-state
& timestamp that would be fixable but for my use case it’s fine for now.
They’re all pretty simple and are used in production at work :)
The latest ‘use-subscription’ on master is alpha, I need to write tests before releasing
Nice. I like the fact you’re using a named function expression.