react

"mulling over state management stuff"
martinklepsch 2020-11-25T00:04:42.041900Z

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?

martinklepsch 2020-11-25T00:22:42.042800Z

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!]))

martinklepsch 2020-11-25T00:58:55.042900Z

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.

lilactown 2020-11-25T03:02:39.043600Z

They’re all pretty simple and are used in production at work :)

lilactown 2020-11-25T03:03:34.044800Z

The latest ‘use-subscription’ on master is alpha, I need to write tests before releasing

ordnungswidrig 2020-11-25T07:44:52.045Z

Nice. I like the fact you’re using a named function expression.