preact-custom-element icon indicating copy to clipboard operation
preact-custom-element copied to clipboard

Duplicating children fix for {shadow: false}

Open AtnesNess opened this issue 4 years ago • 2 comments

When shadowDOM is not attached and developer wants to render children inside react component it will lead to duplicating of children.

  1. Children will be placed at slot as intended
  2. Children will remain at the and of the node, because after rendering original content won't be cleared

AtnesNess avatar Jun 05 '21 02:06 AtnesNess

I just tried the fix shown here and have to admit that it does not work properly: while it indeed deletes one of the two child instances, it removes the first one rather than the second.

As a consequence, componentDidMount will be called for the child instance that will be deleted right afterwards, but never for the instance that "survives". Programs relying on componentDidMount (like mine) will therefore fail...

rozek avatar Sep 08 '23 19:09 rozek

My solution: instead of modifying the source of "preact-custom-components", I got rid of the duplicated child elements using a "trick" mentioned in issue #32:

for a component with inner elements add a <slot/> tag instead of ${props.children}

This workaround sounds strange, but it works (somehow):

  • the <slot/> tag will be rendered as <div><slot></slot></div> and
  • the actual child elements will be rendered as direct descendants of the container element, right next to the <div><slot></slot></div>

This may not look nice, but I can live with that hack...

rozek avatar Sep 08 '23 20:09 rozek