jahilldev / component-elements

Create a custom element from any component with these tiny functions (2KB GZipped, ~1KB Brotli). Preact and React currently supported
MIT License
89 stars 7 forks source link

Cache children and re-use if onConnected called again #19

Closed jahilldev closed 2 years ago

jahilldev commented 2 years ago

Currently when a custom element's onConnected callback is invoked, we're clearing the innerHTML to avoid it interfering with the (p)react component's render structure. If a component is then moved in the DOM, it will trigger the custom elements lifecycle methods again, this time however, there is no nested HTML to assign to this.props.children, so it's undefined on the subsequent render.

The changes in this PR first checks for an assigned value for this.props.children, and makes use of that if already defined, caching the result from the previous render.