New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onremove() gets lost between rerenderings #1994
Comments
Try:
|
@purplecode I think this is by design. You can use keys or a component boundary to get the desired behavior. One caveat with keys, they only work in list context, which is provided implicitly in many places by Mitrhil, but not everywhere. Specifically, they will be ignored when a bare, keyed vnode is returned from a view. You must wrap the returned vnode in an array. |
Thanks, that helps. But it basically means that switch (route) {
case '/a': render(document.body, pageA); break;
case '/b': render(document.body, pageB); break;
} were we have no control on what node will be replaced with. Shall I propose a PR for documentation page? Debugging this might be a real nightmare. |
You can also wrap your pages in components. Another thing with v1 is that diff is skipped it the old vnode Your snippet above wouldn't be able to redraw a given route because the same vnode is fed to You'd probably want something like this. |
Closing as by design. The original snippet removed the relevant attribute hooks on the second render, then removed the node itself, so it'd be expected that they not be called. It's morally equivalent to this:
|
As I mentioned, when you render the whole page, with thousands of DOM nodes, you have no control on what will be replaced by what. I started using unique keys and single item arrays every time with lifecycle callbacks, but is is rather inconvenient. But agree that there is no easy solution. My snippet with routing was just an example. In my real application we use only Since this issue still exists and can be easily mitigated by using components, maybe you could consider removal of the node-specific lifecycle callbacks in the next versions of mithril? |
BTW, I've got a fix for the counter-intuitive behavior here for v2 (I might be able to backport it for v1), but it's pretty restricted. |
If an element is replaced by a new one, lifecycle callbacks become forgotten, and they are never called. It makes integration with third-party libraries highly complicated, because there is no easy way to do cleanups. This might be a design decision, but if so, maybe it could be better documented.
This behavior is also different than in versions < 0.2.8. Previously
config
function was taken into account when doing the nodes comparison andcontext.onunload
was properly called (code below).Expected Behavior
onremove
and other lifecycle callbacks should be taken into account when doing elements comparison, and in case of any difference the node should be regarded as a different one. There is an edge case when<div oncreate={f1} />
is replaced by<div oncreate={f2} />
so both nodes have the same set of hooks, but with different function values, but some compromise could be found.Current Behavior
In the current version
onremove
can be lost during rerenderings and can never be called.For comparison version 0.2.8:
Possible Solution
See expected behavior.
Steps to Reproduce (for bugs)
As in the code snippets.
Context
I tried successfully proxying
hyperscript
method and addingonupdate
on every element, so that I can trigger the hooks manually when I detect the change, but it is very inconvenient and it disables nodes recycling.Your Environment
The text was updated successfully, but these errors were encountered: