https://github.com/preactjs/preact-root-fragment
A standalone Preact 10+ implementation of the deprecated `replaceNode` parameter from Preact 10
https://github.com/preactjs/preact-root-fragment
Last synced: 11 months ago
JSON representation
A standalone Preact 10+ implementation of the deprecated `replaceNode` parameter from Preact 10
- Host: GitHub
- URL: https://github.com/preactjs/preact-root-fragment
- Owner: preactjs
- License: mit
- Created: 2025-07-16T08:09:05.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2025-07-16T09:37:37.000Z (12 months ago)
- Last Synced: 2025-07-17T14:33:35.569Z (12 months ago)
- Language: JavaScript
- Homepage:
- Size: 59.6 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `preact-root-fragment`: partial root rendering for Preact
This is a standalone Preact 10+ implementation of the deprecated `replaceNode` parameter from Preact 10.
It provides a way to render or hydrate a Preact tree using a subset of the children within the parent element passed to render():
```html
⬅ we pass this to render() as the parent DOM element...
⬅ ... but we want to use this tree, not the script
```
### Why do I need this?
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 `` would get hydrated without clobbering the others?
```html
Widget A
Widget B
Widget C
```
Preact 10 provided a somewhat obscure third argument for `render` and `hydrate` called `replaceNode`, which could be used for the above case:
```js
render(, sidebar, widgetA); // render into
, but only look at
render(, sidebar, widgetB); // same, but only look at widgetB
render(, sidebar, widgetC); // same, but only look at widgetC
```
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.
Going forward, we're providing this functionality as a standalone library called `preact-root-fragment`.
### How it works
`preact-root-fragment` provides a `createRootFragment` function:
```ts
createRootFragment(parent: ContainerNode, children: ContainerNode | ContainerNode[]);
```
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.
Using the previous example, we can change the deprecated `replaceNode` usage out for `createRootFragment`:
```js
import { createRootFragment } from 'preact-root-fragment';
render(, createRootFragment(sidebar, widgetA));
render(, createRootFragment(sidebar, widgetB));
render(, createRootFragment(sidebar, widgetC));
```
Since we're creating separate "Persistent Fragment" parents to pass to each `render()` call, Preact will treat each as an independent Virtual DOM tree.
### Multiple Root Elements
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:
```js
import { createRootFragment } from 'preact-root-fragment';
import { render } from 'preact';
function App() {
return (
<>
Example
Hello world!
>
);
}
// Use only the last two child elements within :
const children = [].slice.call(document.body.children, -2);
render(, createRootFragment(document.body, children));
```
### Preact Version Support
This library works with Preact 10 and 11.
### Changelog
#### 0.2.0 (2022-03-04)
- fix bug where nodes were appended instead of replaced (thanks @danielweck)
- fix `.__k` assignment (thanks @danielweck)
- fix Preact 10.6 debug error due to missing `nodeType` (thanks @danielweck)