mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Fix renderer-solid
not creating a reactive root (#848)
* use Solid's render method on the client * add changeset * use createComponent
This commit is contained in:
parent
b8af49f035
commit
bef5103ae3
2 changed files with 17 additions and 11 deletions
5
.changeset/friendly-tigers-smoke.md
Normal file
5
.changeset/friendly-tigers-smoke.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/renderer-solid': patch
|
||||
---
|
||||
|
||||
Uses Solid's `render` function to render our components on the client.
|
|
@ -1,15 +1,16 @@
|
|||
import { createComponent } from 'solid-js/web';
|
||||
import { createComponent } from 'solid-js';
|
||||
import { render } from 'solid-js/web';
|
||||
|
||||
export default (element) => (Component, props) => {
|
||||
// Solid `createComponent` just returns a DOM node with all reactivity
|
||||
// already attached. There's no VDOM, so there's no real need to "mount".
|
||||
// Likewise, `children` can just reuse the nearest `astro-fragment` node.
|
||||
const component = createComponent(Component, {
|
||||
...props,
|
||||
children: element.querySelector('astro-fragment'),
|
||||
});
|
||||
export default (element) => (Component, props, childHTML) => {
|
||||
// Solid's `render` does not replace the element's children.
|
||||
// Deleting the root's children is necessary before calling `render`.
|
||||
element.replaceChildren();
|
||||
|
||||
const children = Array.isArray(component) ? component : [component];
|
||||
const children = document.createElement('astro-fragment');
|
||||
children.innerHTML = childHTML;
|
||||
|
||||
element.replaceChildren(...children);
|
||||
// Using Solid's `render` method ensures that a `root` is created
|
||||
// in order to properly handle reactivity. It also handles
|
||||
// components that are not native HTML elements.
|
||||
render(() => createComponent(Component, { ...props, children }), element);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue