diff --git a/content/en/guide/v10/api-reference.md b/content/en/guide/v10/api-reference.md index c81c1a0e6..dcb414696 100644 --- a/content/en/guide/v10/api-reference.md +++ b/content/en/guide/v10/api-reference.md @@ -69,9 +69,22 @@ render(, document.getElementById('container')); // ``` -If the optional `replaceNode` parameter is provided, it must be a child of `containerNode`. Instead of inferring where to start rendering, Preact will update or replace the passed element using its diffing algorithm. +The first argument must be a valid Virtual DOM Element, which represents either a component or an element. When passing a Component, it's important to let Preact do the instantiation rather than invoking your component directly, which will break in unexpected ways: + +```jsx +const App = () =>
foo
; + +// DON'T: Invoking components directly means they won't be counted as a +// VNode and hence not be able to use functionality relating to vnodes. +render(App(), rootElement); // ERROR +render(App, rootElement); // ERROR + +// DO: Passing components using h() or JSX allows Preact to render correctly: +render(h(App), rootElement); // success +render(, rootElement); // success +``` -> ⚠️ The `replaceNode`-argument will be removed with Preact `v11`. It introduces too many edge cases and bugs which need to be accounted for in the rest of Preact's source code. We're leaving this section up for historical reasons, but we don't recommend anyone to use the third `replaceNode` argument. +If the optional `replaceNode` parameter is provided, it must be a child of `containerNode`. Instead of inferring where to start rendering, Preact will update or replace the passed element using its diffing algorithm. ```jsx // DOM tree before render: @@ -97,20 +110,7 @@ render( // ``` -The first argument must be a valid Virtual DOM Element, which represents either a component or an element. When passing a Component, it's important to let Preact do the instantiation rather than invoking your component directly, which will break in unexpected ways: - -```jsx -const App = () =>
foo
; - -// DON'T: Invoking components directly means they won't be counted as a -// VNode and hence not be able to use functionality relating to vnodes. -render(App(), rootElement); // ERROR -render(App, rootElement); // ERROR - -// DO: Passing components using h() or JSX allows Preact to render correctly: -render(h(App), rootElement); // success -render(, rootElement); // success -``` +> ⚠️ The `replaceNode`-argument will be removed with Preact `v11`. It introduces too many edge cases and bugs which need to be accounted for in the rest of Preact's source code. If you still need this functionality, we recommend using [`preact-root-fragment`](/guide/v10/preact-root-fragment), a small helper library that provides similar functionality. It is compatible with both Preact `v10` and `v11`. ## hydrate() diff --git a/content/en/guide/v10/preact-root-fragment.md b/content/en/guide/v10/preact-root-fragment.md new file mode 100644 index 000000000..ccb9c022a --- /dev/null +++ b/content/en/guide/v10/preact-root-fragment.md @@ -0,0 +1,102 @@ +--- +title: preact-root-fragment +description: A standalone Preact 10+ implementation of the deprecated `replaceNode` parameter from Preact 10 +--- + +# preact-root-fragment + +preact-root-fragment is a standalone and more flexible 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 + +``` + +Preact 10 provided a somewhat obscure third argument for `render` and `hydrate` called `replaceNode`, which could be used for the above case: + +```jsx +render(, sidebar, widgetA); // render into