Giter Club home page Giter Club logo

Comments (4)

tonsky avatar tonsky commented on July 19, 2024

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?

from rum.

kurt-o-sys avatar kurt-o-sys commented on July 19, 2024

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.

from rum.

tonsky avatar tonsky commented on July 19, 2024

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.

from rum.

kurt-o-sys avatar kurt-o-sys commented on July 19, 2024

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!

from rum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.