Fork me on GitHub
#helix
<
2021-07-12
>
kennytilton07:07:44

I am leaning towards making Helix the basis of my new CLJS+RN library based on my own state management library, and I am still wrapping my head around RN itself so this may be wrong, but I think I need to use some form of create-class to be able to force updates when my state management lib sees the need. This doc is great https://github.com/lilactown/helix/blob/master/docs/creating-components.md but ends up with class components as "WIP". Is there any progress on that? I will keep staring at RN to see if a supervising engine can force the re-render of functional components -- hints welcome! 🙏

lilactown16:07:45

hey kenny! i'll read through the issue you created and your comments on helix-todo-mvc this afternoon.

lilactown16:07:42

I will reply re: classes- with hooks, you should not need classes except if you are trying to build an https://reactjs.org/docs/error-boundaries.htmly

lilactown16:07:21

there's a macro for defining classes called defcomponent in helix.core but I just haven't documented it

lilactown17:07:58

the way that you would have your state management lib trigger a render is by using a custom hook in the body of the component listening to the observable that updates the state using some other hook. helix.hooks/use-subscription can cover this use-case in stable react versions today. here's a rough sketch:

(defhook use-observable
  [rx]
  (hooks/use-subscription
    (hooks/use-memo
     [rx]
     {:get-current-value #(deref rx)
      :subscribe (fn [callback]
                   (let [unsub (subscribe! rx callback)]
                      ;; function to call to unsubscribe when unmounting or `rx` changes
                      unsub))})))


(defnc some-component
  []
  (let [user-name (use-observable user-name-rx)]
    (d/div "Hello, " user-name)))

kennytilton19:07:43

Awesome. I had my eyes on the error-boundary example, thought I saw the control I needed, but perhaps it would be less brittle to use hooks. I suspect performance will be excellent since they work so hard on that. Thx, @lilactown!