|
| 1 | +# `preact-root-fragment`: partial root rendering for Preact |
| 2 | + |
| 3 | +This is a standalone Preact 10+ implementation of the deprecated `replaceNode` parameter from Preact 10. |
| 4 | + |
| 5 | +It provides a way to render or hydrate a Preact tree using a subset of the children within the parent element passed to render(): |
| 6 | + |
| 7 | +```html |
| 8 | +<body> |
| 9 | + <div id="root"> |
| 10 | + ⬅ we pass this to render() as the parent DOM element... |
| 11 | + |
| 12 | + <script src="/etc.js"></script> |
| 13 | + |
| 14 | + <div class="app"> |
| 15 | + ⬅ ... but we want to use this tree, not the script |
| 16 | + <!-- ... --> |
| 17 | + </div> |
| 18 | + </div> |
| 19 | +</body> |
| 20 | +``` |
| 21 | + |
| 22 | +### Why do I need this? |
| 23 | + |
| 24 | +This is particularly useful for [partial hydration](https://jasonformat.com/islands-architecture/), which often requires rendering multiple distinct Preact trees into the same parent DOM element. Imagine the scenario below - which elements would we pass to `hydrate(jsx, parent)` such that each widget's `<section>` would get hydrated without clobbering the others? |
| 25 | + |
| 26 | +```html |
| 27 | +<div id="sidebar"> |
| 28 | + <section id="widgetA"><h1>Widget A</h1></section> |
| 29 | + <section id="widgetB"><h1>Widget B</h1></section> |
| 30 | + <section id="widgetC"><h1>Widget C</h1></section> |
| 31 | +</div> |
| 32 | +``` |
| 33 | + |
| 34 | +Preact 10 provided a somewhat obscure third argument for `render` and `hydrate` called `replaceNode`, which could be used for the above case: |
| 35 | + |
| 36 | +```js |
| 37 | +render(<A />, sidebar, widgetA); // render into <div id="sidebar">, but only look at <section id="widgetA"> |
| 38 | +render(<B />, sidebar, widgetB); // same, but only look at widgetB |
| 39 | +render(<C />, sidebar, widgetC); // same, but only look at widgetC |
| 40 | +``` |
| 41 | + |
| 42 | +While the `replaceNode` argument proved useful for handling scenarios like the above, it was limited to a single DOM element and could not accommodate Preact trees with multiple root elements. It also didn't handle updates well when multiple trees were mounted into the same parent DOM element, which turns out to be a key usage scenario. |
| 43 | + |
| 44 | +Going forward, we're providing this functionality as a standalone library called `preact-root-fragment`. |
| 45 | + |
| 46 | +### How it works |
| 47 | + |
| 48 | +`preact-root-fragment` provides a `createRootFragment` function: |
| 49 | + |
| 50 | +```ts |
| 51 | +createRootFragment(parent: ContainerNode, children: ContainerNode | ContainerNode[]); |
| 52 | +``` |
| 53 | + |
| 54 | +Calling this function with a parent DOM element and one or more child elements returns a "Persistent Fragment". A persistent fragment is a fake DOM element, which pretends to contain the provided children while keeping them in their existing real parent element. It can be passed to `render()` or `hydrate()` instead of the `parent` argument. |
| 55 | + |
| 56 | +Using the previous example, we can change the deprecated `replaceNode` usage out for `createRootFragment`: |
| 57 | + |
| 58 | +```js |
| 59 | +import { createRootFragment } from 'preact-root-fragment'; |
| 60 | + |
| 61 | +render(<A />, createRootFragment(sidebar, widgetA)); |
| 62 | +render(<B />, createRootFragment(sidebar, widgetB)); |
| 63 | +render(<C />, createRootFragment(sidebar, widgetC)); |
| 64 | +``` |
| 65 | + |
| 66 | +Since we're creating separate "Persistent Fragment" parents to pass to each `render()` call, Preact will treat each as an independent Virtual DOM tree. |
| 67 | + |
| 68 | +### Multiple Root Elements |
| 69 | + |
| 70 | +Unlike the `replaceNode` parameter from Preact 10, `createRootFragment` can accept an Array of children that will be used as the root elements when rendering. This is particularly useful when rendering a Virtual DOM tree that produces multiple root elements, such as a Fragment or an Array: |
| 71 | + |
| 72 | +```js |
| 73 | +import { createRootFragment } from 'preact-root-fragment'; |
| 74 | +import { render } from 'preact'; |
| 75 | + |
| 76 | +function App() { |
| 77 | + return ( |
| 78 | + <> |
| 79 | + <h1>Example</h1> |
| 80 | + <p>Hello world!</p> |
| 81 | + </> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +// Use only the last two child elements within <body>: |
| 86 | +const children = [].slice.call(document.body.children, -2); |
| 87 | + |
| 88 | +render(<App />, createRootFragment(document.body, children)); |
| 89 | +``` |
| 90 | + |
| 91 | +### Preact Version Support |
| 92 | + |
| 93 | +This library works with Preact 10 and 11. |
| 94 | + |
| 95 | +### Changelog |
| 96 | + |
| 97 | +#### 0.2.0 (2022-03-04) |
| 98 | + |
| 99 | +- fix bug where nodes were appended instead of replaced (thanks @danielweck) |
| 100 | +- fix `.__k` assignment (thanks @danielweck) |
| 101 | +- fix Preact 10.6 debug error due to missing `nodeType` (thanks @danielweck) |
0 commit comments