Comments (6)
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.
@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.
@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.
@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.
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.
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:
vecty.Rerender
invokesdoRestore
with the component's previousComponent.Render
and the newComponent.Render
as parameters.doRestore
effectively just invokes(*HTML).Restore
(*HTML).Restore
makes one of two decisions:- Can we reuse (read: just modify) the old browser DOM element to match the new render? If so it delegates to
(*HTML).restoreHTML
- Do we need to create the browser DOM element from scratch (i.e. is the previous render nil/non-existent)? It creates the real DOM element if so.
- Can we reuse (read: just modify) the old browser DOM element to match the new render? If so it delegates to
(*HTML).restoreHTML
s goal is to restore or reinstate the friendly relationship between the new render (theh
method receiver) and the previous/old render (theprev
method parameter):- It begins by immediately using the same browser HTML node instead of creating a new one.
- It then determines what properties have been modified, and sets those on the existing browser DOM node
- It removes any properties from the node that existed in the old render but not the new one
- It does steps 2 & 3 above for attributes, then styles, event listeners (oversimplification), and unsafe HTML if present.
- 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:
- 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
- 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 ap
then Vecty (and React) must destroy thediv
and create a newp
.
Does this all belong in some documentation? You bet it does! And it will be soon :)
from vecty.
Related Issues (20)
- Add a basic example on usage HOT 5
- allow global panic handler HOT 2
- removeChild crashes if parent node is nil HOT 1
- [feature request] static HTML/CSS components HOT 6
- Examples go.mod file conflict HOT 1
- Add snake videogame example. HOT 4
- question: What is this prop vs. state idea HOT 3
- Instrumenting panic messages HOT 8
- question: initializing rendered HTML nodes via javascript HOT 4
- Porting gopherjs js calls to use with vecty HOT 2
- Document the availability of vecty project template generator HOT 1
- Return type of If function
- prop.Value("") does not produce the correct html on first render
- Vecty needs an official website HOT 10
- The slack invite link in readme is broken
- vecty docs HOT 1
- How to update a rendered element? HOT 10
- Failed Running The Example HOT 8
- Todo MVC example has misleading usage of p.Items HOT 2
- Rerendering List Replaces First ListItem HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vecty.