Closed kurt-o-sys closed 9 years ago
Old and new are states of component. In simplest case, it you’re not using anything fancy, state just contains arguments this component was created with ({:rum/args args}
). By comparing args it was created before and args it is created now, you can decide if you need to update.
Simple way to solve your problem is to call component with query result as argument:
(comp (ds/q ...))
and use rum/static
mixin which does exactly that: compares old arguments value with new arguments value.
Did I answered your question?
Here's how I did it so far. The calling component passes an entity, not query result. The reason is I needed the entity as well. It seemed logic to me to pass the entity only, since that would/should be enough:
(rum/defc stagelist []
[:section
(let [q (->> (ds/q '[:find ?e ?o :where [?e :stage/order ?o] [?e :stage/name ?n]] @conn)
(sort-by second))]
(map #(stage (ds/entity @conn (first %))) q))])
The component that's being called (the one with the query):
(rum/defc stage [ent]
(let [n (name (:stage/name ent))
id (:db/id ent)
q (ds/q `[:find [?e ...] :where [?e :item/stage ~id]] @conn)]
... ))
The passed entity itself is not changing, but the result of the query is. I didn't add any mixin in this example. I nest and use this kind of 'pass entity and perform a query' rather often, or at least, I would like to. Since the query is something 'component specific', I'd rather keep in in the component, not in the caller. This way, it makes the code easier to reason about imo - the query is bounded to the component.
Well, there’s no way around it. If you want to avoid unnecessary rendering you need a component with a lifecycle around the place. In your case you probably need second component that accepts query result and implements shouldComponentUpdate
. Then just include that component inside stage
:
(rum/defc stage-inner < rum/static [q n]
...)
(rum/defc stage [ent]
(let [n (name (:stage/name ent))
id (:db/id ent)
q (ds/q `[:find [?e ...] :where [?e :item/stage ~id]] @conn)]
(stage-inner q n)))
Remember that you’re not avoiding query. You only avoiding rendering of stage-inner
. Worth it only if rendering is expensive.
Right, I started to realize that :). Of course, the query can't be avoided, rendering can... which was my goal (just to get a hang of rum). I rather like the inner component idea. It's simple, and logic. It'll be pretty easy to make a function or macro to generate both, and possibly adding some functionality in the outer/wrapper component.
Great, thx a lot!
I'm playing around with rum, using the idea of https://github.com/tonsky/datascript-chat - I'm omtting the async/chan part for this example. The application is rather heavily query-based, and I want to implement a :should-update mixin, which will update the components only if the queries are changed (sounds pretty obvious). I have something like:
Any updates in the database trigger a rum/request-render. Now, the 'should-update' mixin looks like:
However, the 'old' state and the 'new' state are not the old and new state of the transaction (or the component as such), but rather some intermediate states of the component (so it seems to me). The old value is not the 'previous query result' or 'previous state which led to that query result'. What I'd like is to compare the previous result of the query with the new result of the query.
What's the most logic/best way to do this using rum?