Giter Site home page Giter Site logo

Comments (6)

pdf avatar pdf commented on May 17, 2024 1

Thank you very much for the detailed response, I missed the delegation to restoreHTML, and reassignment of the Node, makes all the difference!

from vecty.

slimsag avatar slimsag commented on May 17, 2024

@pdf Vecty already supports reconciliation exactly the same way React does. For an example of this see the TodoMVC example: https://github.com/gopherjs/vecty/blob/master/examples/todomvc/components/pageview.go#L24-L29

Also see the godoc for the Restorer interface (https://godoc.org/github.com/gopherjs/vecty#Restorer) that should be implemented by your component in order to handle restoration (reconciliation).

I think this is just a case of missing docs, so closing in favor of #31 -- please reopen/comment if you feel otherwise :)

from vecty.

pdf avatar pdf commented on May 17, 2024

@slimsag did you have a read of the linked article? That should explain what I'm talking about here.

As an aside, I was aware of the Restorer interface, but that's not the same thing. On that topic though, I'm not sure calling the function Restore is ideal, and the return bool is lexically backwards - Restore -> true actually means don't restore the element, pretty confusing.

from vecty.

slimsag avatar slimsag commented on May 17, 2024

@pdf Sorry for the confusion. I did misread your original post.

However, the Restorer interface is used internally by Vecty already in order to restore *HTML structs by calculating the difference between the previous render and applying the modifications (instead of creating new elements) just as described in the React reconcilliation document you linked (https://facebook.github.io/react/docs/reconciliation.html).

The relevant code is here: https://github.com/gopherjs/vecty/blob/master/dom.go#L98-L193

The one case where we don't currently do well, however, is with reusing list elements, and that is issue #25

from vecty.

pdf avatar pdf commented on May 17, 2024

So, doRestore applies from the old node to the new node, then the new node replaces the old node in the dom in most instances? Do I have the right reading of the code here?

If so, isn't that the opposite of what should happen, which is that the new render is generated, then applied onto the old node when of a compatible type, to avoid tearing down the whole tree for every update?

from vecty.

slimsag avatar slimsag commented on May 17, 2024

So, doRestore applies from the old node to the new node, then the new node replaces the old node in the dom in most instances? Do I have the right reading of the code here?

lol that is obviously not what it does I hope :P that would defeat the entire purpose of doing things like this, as you already said. The correct reading of the code would be something like:

  1. vecty.Rerender invokes doRestore with the component's previous Component.Render and the new Component.Render as parameters.
  2. doRestore effectively just invokes (*HTML).Restore
  3. (*HTML).Restore makes one of two decisions:
  4. (*HTML).restoreHTMLs goal is to restore or reinstate the friendly relationship between the new render (the h method receiver) and the previous/old render (the prev method parameter):
    1. It begins by immediately using the same browser HTML node instead of creating a new one.
    2. It then determines what properties have been modified, and sets those on the existing browser DOM node
    3. It removes any properties from the node that existed in the old render but not the new one
    4. It does steps 2 & 3 above for attributes, then styles, event listeners (oversimplification), and unsafe HTML if present.
  5. It then proceeds to do the same thing for all of that component (the *HTML)s children, recursively (oversimplification)

There's more here to cover, like my oversimplification of event listener management and child component recursion, but the point is this:

Yes, Vecty will always reuse the same DOM element in all cases except:

  1. If the render was a list of elements and they are no longer in the same order, for this we would need 'keys' like React has. See issue #25
  2. If the actual browser DOM element tag has changed, e.g. if what you previously rendered was a div and what you've rendered now is a p then Vecty (and React) must destroy the div and create a new p.

Does this all belong in some documentation? You bet it does! And it will be soon :)

from vecty.

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.