Preact: this.base gets recycled while component is mounted?

Created on 13 Jun 2016  路  7Comments  路  Source: preactjs/preact

Hey,
this.base gets recycled and reused somewhere else while the a component is mounted, this breaks things for me as I bind events to this.base

this only happens in beta11 not beta10 probably due to 8790b2de4fbd38eb3277cbfc7e609f5332ebbc4d

I could bind my events in some other way but don't see a good way to do it except this.base.addEventLister/this.base.removeEventListener

thanks for any help

bug

All 7 comments

Interesting. I'm not sure if that's an unmount bug, or just the intended behavior being corrected.

For your situation, are you rendering a component as the child? If so I would suggest passing your event handlers down to it as props. Preact (and as far as I can tell React) treats High Order components as simple shells around a concrete component, which means the event handlers for a given component are owned by the innermost HOC.

I'll see if I can find a case where unmount isn't supposed to happen, the commit you linked does appear to be related.

The unmount on the component in question actually does not happen (at least the function is not called) but the element that is this.base during componentDidMount ends up not being the root of the component but one of its children

Ah I saw this on Saturday and wondered. Sounds like the same issue! It's insanely difficult to pin down in tests but I do have a repro that will let me set breakpoints and figure out what is triggering recycling.

Great to hear that! If you need additional testing I can run it on my code as well, have one scenario it repros every time.

Though it is very weird as it is a component I use on pretty much on every page and all other seem to be just fine

The only thing I could think of is I am using 2 copies on this page nested with a bunch of components in between

馃敤 Found the proper fix! My repro was a <Router /> wrapping multiple instances of <Preview />, and re-rendering the <Preview /> with different props was incorrectly unmounting Router:

tested with beta12 and it works :+1:

Awesome! I had to tweak the version a bit, apparently I've been using invalid semver (beyond the obvious piling beta releases into 5.0.0, which I regret...) - the most recent version is now properly formatted but required bumping patch: 5.0.1-beta.12.

I'll close this for now, but this is another reason 5.x needs to get rolled out as stable ASAP!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adriaanwm picture adriaanwm  路  3Comments

skaraman picture skaraman  路  3Comments

simonjoom picture simonjoom  路  3Comments

SabirAmeen picture SabirAmeen  路  3Comments

nopantsmonkey picture nopantsmonkey  路  3Comments