Giter Club home page Giter Club logo

Comments (9)

Azzurite avatar Azzurite commented on August 20, 2024

And whatever the answer for "why is it updating 3 times" is, I'd personally also like to know how to avoid this :)

from rum.

roman01la avatar roman01la commented on August 20, 2024

This is a React question. React batches updates scheduled synchronously in React-controlled phases, such as event handling in this case. When called outside of such controlled phases, such as asynchronously in an event handler in this case, React is not able to batch scheduled updates, thus component update is triggered for every scheduled update. For those cases one can use React's batching API (batchedUpdates), to explicitly batch updates that are out of React's control

from rum.

Jarzka avatar Jarzka commented on August 20, 2024

That's interesting, because the same example in Reagent only renders the component once, even if I update the atoms asynchronously:

(def value1 (r/atom 1))
(def value2 (r/atom 10))
(def value3 (r/atom 100))

(defn render-example []
          (println "Rendering example")
          [:<>
           [:div (str "Values: " @value1 ", " @value2 " and " @value3)]
           [:button {:on-click (fn []
                                 (go
                                   (swap! value1 inc)
                                   (swap! value2 inc)
                                   (swap! value3 inc)))}
            "Increment all (async)"]])

I wonder if Reagent has done some optimisations when multiple Reagent atoms are being updated, so that the component is not rendered between the updates.

from rum.

roman01la avatar roman01la commented on August 20, 2024

Yes, Reagent is using its own scheduling and processing queue, Rum used to have that as well, but that was changed to be closer to React, be compatible with concurrent mode and avoid issue with input fields

from rum.

Jarzka avatar Jarzka commented on August 20, 2024

Okay, good to know. Thanks for the answer.

from rum.

Azzurite avatar Azzurite commented on August 20, 2024

The thing though is, how is it possible to batch your own updates when using the reactive mixin? Wouldn't the mixin need to do that? It'd be a bit weird if in fact the solution is to program your own reactive mixin.

from rum.

roman01la avatar roman01la commented on August 20, 2024

batching happens on caller's side

(react-dom/unstable_batchedUpdates
  (fn []
    (swap! value1 inc)
    (swap! value2 inc)))

from rum.

Azzurite avatar Azzurite commented on August 20, 2024

I see, thank you very much :) I didn't understand how that feature worked when reading about it.

from rum.

Azzurite avatar Azzurite commented on August 20, 2024

@Jarzka is this closeable?

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.