Fork me on GitHub
#clojurescript
<
2022-11-30
>
Steve Pegoraro00:11:29

Hi all, I’m using rum+datascript in a frontend application with core.async as an event bus (like @tonsky’s cat chat demo) which is awesome but I’m having some issues with the entire component tree being re-rendered on each change to the db (it causes inputs to be re-created when they are children, this leads to them losing focus while the user types). Has anyone come up with a nice solution so that components in the tree can subscribe to only update when certain entities/queries change? Similar to using an atom and cursor basically but with datascript.

Steve Pegoraro01:11:03

For some extra info I’ve been working on a mixin like this but don’t quite have it yet:

(defn react-on [attr]
  {:did-mount (fn [state]
                (d/listen! conn (fn [tx-report]
                                  (when (some #(= (nth % 1) attr) (:tx-data tx-report))
                                    (println (str "attribute " attr " changed"))
                                    (rum/request-render (:rum/react-component state))))))})
The idea being to define a component and dependent attribute (or query - to be implemented later) like this:
(rum/defc my-component < (react-on :some/attr) [])