A virtual DOM library with focus on simplicity, modularity, powerful featur...



A virtual DOM library with focus on simplicity, modularity, powerful features
and performance.

License: MIT Build Status npm version npm downloads Join the chat at
Donate to our collective

Thanks to Browserstack for providing access to
their great cross-browser testing tools.

English | 简体中文


Virtual DOM is awesome. It allows us to express our application's view
as a function of its state. But existing solutions were way way too
bloated, too slow, lacked features, had an API biased towards OOP
and/or lacked features I needed.

Snabbdom consists of an extremely simple, performant and extensible
core that is only ≈ 200 SLOC. It offers a modular architecture with
rich functionality for extensions through custom modules. To keep the
core simple, all non-essential functionality is delegated to modules.

You can mold Snabbdom into whatever you desire! Pick, choose and
customize the functionality you want. Alternatively you can just use
the default extensions and get a virtual DOM library with high
performance, small size and all the features listed below.


- Core features
  - About 200 SLOC – you could easily read through the entire core and fully
    understand how it works.
  - Extendable through modules.
  - A rich set of hooks available, both per vnode and globally for modules,
    to hook into any part of the diff and patch process.
  - Splendid performance. Snabbdom is among the fastest virtual DOM libraries.
  - Patch function with a function signature equivalent to a reduce/scan
    function. Allows for easier integration with a FRP library.
- Features in modules
  - h function for easily creating virtual DOM nodes.
  - [SVG _just works_ with the h helper](#svg).
  - Features for doing complex CSS animations.
  - Powerful event listener functionality.
  - Thunks to optimize the diff and patch process even further.
- Third party features
  - Server-side HTML output provided by snabbdom-to-html.
  - Compact virtual DOM creation with snabbdom-helpers.
  - Template string support using snabby.
  - Virtual DOM assertion with snabbdom-looks-like


  1. ```mjs
  2. import {
  3.   init,
  4.   classModule,
  5.   propsModule,
  6.   styleModule,
  7.   eventListenersModule,
  8.   h,
  9. } from "snabbdom";

  10. const patch = init([
  11.   // Init patch function with chosen modules
  12.   classModule, // makes it easy to toggle classes
  13.   propsModule, // for setting properties on DOM elements
  14.   styleModule, // handles styling on elements with support for animations
  15.   eventListenersModule, // attaches event listeners
  16. ]);

  17. const container = document.getElementById("container");

  18. const vnode = h("div#container.two.classes", { on: { click: someFn } }, [
  19.   h("span", { style: { fontWeight: "bold" } }, "This is bold"),
  20.   " and this is just normal text",
  21.   h("a", { props: { href: "/foo" } }, "I'll take you places!"),
  22. ]);
  23. // Patch into empty DOM element – this modifies the DOM as a side effect
  24. patch(container, vnode);

  25. const newVnode = h(
  26.   "div#container.two.classes",
  27.   { on: { click: anotherEventHandler } },
  28.   [
  29.     h(
  30.       "span",
  31.       { style: { fontWeight: "normal", fontStyle: "italic" } },
  32.       "This is now italic type"
  33.     ),
  34.     " and this is still just normal text",
  35.     h("a", { props: { href: "/bar" } }, "I'll take you places!"),
  36.   ]
  37. );
  38. // Second `patch` invocation
  39. patch(vnode, newVnode); // Snabbdom efficiently updates the old view to the new state
  40. ```

More examples

Table of contents

  - [init](#init)
  - [patch](#patch)
    - Unmounting
  - [h](#h)
  - [fragment](#fragment-experimental) (experimental)
  - [tovnode](#tovnode)
  - Hooks
    - Overview
    - Usage
    - [The init hook](#the-init-hook)
    - [The insert hook](#the-insert-hook)
    - [The remove hook](#the-remove-hook)
    - [The destroy hook](#the-destroy-hook)
    - [Set properties on remove](#set-properties-on-remove)
    - [Set properties on destroy](#set-properties-on-destroy)
  - Babel
- [children : Array](#children--arrayvnode)

Core documentation

The core of Snabbdom provides only the most essential functionality.
It is designed to be as simple as possible while still being fast and


The core exposes only one single function init. This init
takes a list of modules and returns a patch function that uses the
specified set of modules.

  1. ```mjs
  2. import { classModule, styleModule } from "snabbdom";

  3. const patch = init([classModule, styleModule]);
  4. ```


The patch function returned by init takes two arguments. The first
is a DOM element or a vnode representing the current view. The second
is a vnode representing the new, updated view.

If a DOM element with a parent is passed, newVnode will be turned
into a DOM node, and the passed element will be replaced by the
created DOM node. If an old vnode is passed, Snabbdom will efficiently
modify it to match the description in the new vnode.

Any old vnode passed must be the resulting vnode from a previous call
to patch. This is necessary since Snabbdom stores information in the
vnode. This makes it possible to implement a simpler and more
performant architecture. This also avoids the creation of a new old
vnode tree.

  1. ```mjs
  2. patch(oldVnode, newVnode);
  3. ```


While there is no API specifically for removing a VNode tree from its mount point element, one way of almost achieving this is providing a comment VNode as the second argument to patch, such as:

  1. ```mjs
  2. patch(
  3.   oldVnode,
  4.   h("!", {
  5.     hooks: {
  6.       post: () => {
  7.         /* patch complete */
  8.       },
  9.     },
  10.   })
  11. );
  12. ```

Of course, then there is still a single comment node at the mount point.


It is recommended that you use h to create vnodes. It accepts a
tag/selector as a string, an optional data object and an optional string or
array of children.

  1. ```mjs
  2. import { h } from "snabbdom";

  3. const vnode = h("div", { style: { color: "#000" } }, [
  4.   h("h1", "Headline"),
  5.   h("p", "A paragraph"),
  6. ]);
  7. ```

fragment (experimental)

Caution: This feature is currently experimental and must be opted in.
Its API may be changed without an major version bump.

  1. ```mjs
  2. const patch = init(modules, undefined, {
  3.   experimental: {
  4.     fragments: true,
  5.   },
  6. });
  7. ```

Creates a virtual node that will be converted to a document fragment containing the given children.

  1. ```mjs
  2. import { fragment, h } from "snabbdom";

  3. const vnode = fragment(["I am", h("span", [" a", " fragment"])]);
  4. ```


Converts a DOM node into a virtual node. Especially good for patching over an pre-existing,
server-side generated content.

  1. ```mjs
  2. import {
  3.   init,
  4.   classModule,
  5.   propsModule,
  6.   styleModule,
  7.   eventListenersModule,
  8.   h,
  9.   toVNode,
  10. } from "snabbdom";

  11. const patch = init([
  12.   // Init patch function with chosen modules
  13.   classModule, // makes it easy to toggle classes
  14.   propsModule, // for setting properties on DOM elements
  15.   styleModule, // handles styling on elements with support for animations
  16.   eventListenersModule, // attaches event listeners
  17. ]);

  18. const newVNode = h("div", { style: { color: "#000" } }, [
  19.   h("h1", "Headline"),
  20.   h("p", "A paragraph"),
  21. ]);

  22. patch(toVNode(document.querySelector(".container")), newVNode);
  23. ```


Hooks are a way to hook into the lifecycle of DOM nodes. Snabbdom
offers a rich selection of hooks. Hooks are used both by modules to
extend Snabbdom, and in normal code for executing arbitrary code at
desired points in the life of a virtual node.



The following hooks are available for modules: pre, create,
update, destroy, remove, post.

The following hooks are available in the hook property of individual
elements: init, create, insert, prepatch, update,
postpatch, destroy, remove.


To use hooks, pass them as an object to hook field of the data
object argument.

  1. ```mjs
  2. h("div.row", {
  3.   key: movie.rank,
  4.   hook: {
  5.     insert: (vnode) => {
  6.       movie.elmHeight = vnode.elm.offsetHeight;
  7.     },
  8.   },
  9. });
  10. ```

The init hook

This hook is invoked during the patch process when a new virtual node
has been found. The hook is called before Snabbdom has processed the
node in any way. I.e., before it has created a DOM node based on the

The insert hook

This hook is invoked once the DOM element for a vnode has been
inserted into the document _and_ the rest of the patch cycle is done.
This means that you can do DOM measurements (like using
in this hook safely, knowing that no elements will be changed
afterwards that could affect the position of the inserted elements.

The remove hook

Allows you to hook into the removal of an element. The hook is called
once a vnode is to be removed from the DOM. The handling function
receives both the vnode and a callback. You can control and delay the
removal with the callback. The callback should be invoked once the
hook is done doing its business, and the element will only be removed
once all remove hooks have invoked their callback.

The hook is only triggered when an element is to be removed from its
parent – not if it is the child of an element that is removed. For
that, see the destroy hook.

The destroy hook

This hook is invoked on a virtual node when its DOM element is removed
from the DOM or if its parent is being removed from the DOM.

To see the difference between this hook and the remove hook,
consider an example.

  1. ```mjs
  2. const vnode1 = h("div", [h("div", [h("span", "Hello")])]);
  3. const vnode2 = h("div", []);
  4. patch(container, vnode1);
  5. patch(vnode1, vnode2);
  6. ```

Here destroy is triggered for both the inner div element _and_ the
span element it contains. remove, on the other hand, is only
triggered on the div element because it is the only element being
detached from its parent.

You can, for instance, use remove to trigger an animation when an
element is being removed and use the destroy hook to additionally
animate the disappearance of the removed element's children.

Creating modules

Modules works by registering global listeners for hooks. A module is simply a dictionary mapping hook names to functions.

  1. ```mjs
  2. const myModule = {
  3.   create: function (oldVnode, vnode) {
  4.     // invoked whenever a new virtual node is created
  5.   },
  6.   update: function (oldVnode, vnode) {
  7.     // invoked whenever a virtual node is updated
  8.   },
  9. };
  10. ```

With this mechanism you can easily augment the behaviour of Snabbdom.
For demonstration, take a look at the implementations of the default

Modules documentation

This describes the core modules. All modules are optional. JSX examples assume you're using the [jsx pragma](#jsx) provided by this library.

The class module

The class module provides an easy way to dynamically toggle classes on
elements. It expects an object in the class data property. The
object should map class names to booleans that indicates whether or
not the class should stay or go on the vnode.

  1. ```mjs
  2. h("a", { class: { active: true, selected: false } }, "Toggle");
  3. ```

In JSX, you can use class like this:

  1. ``` js
  2. <div class={{ foo: true, bar: true }} />
  3. // Renders as:
  4. ```

The props module

Allows you to set properties on DOM elements.

  1. ```mjs
  2. h("a", { props: { href: "/foo" } }, "Go to Foo");
  3. ```

In JSX, you can use props like this:

  1. ``` js
  2. <input props={{ name: "foo" }} />
  3. // Renders as: with === "foo"
  4. ```

Properties can only be set. Not removed. Even though browsers allow addition and
deletion of custom properties, deletion will not be attempted by this module.
This makes sense, because native DOM properties cannot be removed. And
if you are using custom properties for storing values or referencing
objects on the DOM, then please consider using
instead. Perhaps via the dataset module.

The attributes module

Same as props, but set attributes instead of properties on DOM elements.

  1. ```mjs
  2. h("a", { attrs: { href: "/foo" } }, "Go to Foo");
  3. ```

In JSX, you can use attrs like this:

  1. ``` js
  2. <div attrs={{ "aria-label": "I'm a div" }} />
  3. // Renders as:
  4. ```

Attributes are added and updated using setAttribute. In case of an
attribute that had been previously added/set and is no longer present
in the attrs object, it is removed from the DOM element's attribute
list using removeAttribute.

In the case of boolean attributes (e.g. disabled, hidden,
selected ...), the meaning doesn't depend on the attribute value
(true or false) but depends instead on the presence/absence of the
attribute itself in the DOM element. Those attributes are handled
differently by the module: if a boolean attribute is set to a
(0, -0, null, false,NaN, undefined, or the empty string
("")), then the attribute will be removed from the attribute list of
the DOM element.

The dataset module

Allows you to set custom data attributes (data-*) on DOM elements. These can then be accessed with the HTMLElement.dataset property.

  1. ```mjs
  2. h("button", { dataset: { action: "reset" } }, "Reset");
  3. ```

In JSX, you can use dataset like this:

  1. ``` js
  2. <div dataset={{ foo: "bar" }} />
  3. // Renders as:
  4. ```

The style module

The style module is for making your HTML look slick and animate smoothly. At
its core it allows you to set CSS properties on elements.

  1. ```mjs
  2. h(
  3.   "span",
  4.   {
  5.     style: {
  6.       border: "1px solid #bada55",
  7.       color: "#c0ffee",
  8.       fontWeight: "bold",
  9.     },
  10.   },
  11.   "Say my name, and every colour illuminates"
  12. );
  13. ```

In JSX, you can use style like this:

  1. ``` js
  2. <div
  3.   style={{
  4.     border: "1px solid #bada55",
  5.     color: "#c0ffee",
  6.     fontWeight: "bold",
  7.   }}
  8. />
  9. // Renders as:
  10. ```

Custom properties (CSS variables)

CSS custom properties (aka CSS variables) are supported, they must be prefixed
with --

  1. ```mjs
  2. h(
  3.   "div",
  4.   {
  5.     style: { "--warnColor": "yellow" },
  6.   },
  7.   "Warning"
  8. );
  9. ```

Delayed properties

You can specify properties as being delayed. Whenever these properties
change, the change is not applied until after the next frame.

  1. ```mjs
  2. h(
  3.   "span",
  4.   {
  5.     style: {
  6.       opacity: "0",
  7.       transition: "opacity 1s",
  8.       delayed: { opacity: "1" },
  9.     },
  10.   },
  11.   "Imma fade right in!"
  12. );
  13. ```

This makes it easy to declaratively animate the entry of elements.

The all value of transition-property is not supported.

Set properties on remove

Styles set in the remove property will take effect once the element
is about to be removed from the DOM. The applied styles should be
animated with CSS transitions. Only once all the styles are done
animating will the element be removed from the DOM.

  1. ```mjs
  2. h(
  3.   "span",
  4.   {
  5.     style: {
  6.       opacity: "1",
  7.       transition: "opacity 1s",
  8.       remove: { opacity: "0" },
  9.     },
  10.   },
  11.   "It's better to fade out than to burn away"
  12. );
  13. ```

This makes it easy to declaratively animate the removal of elements.

The all value of transition-property is not supported.

Set properties on destroy

  1. ```mjs
  2. h(
  3.   "span",
  4.   {
  5.     style: {
  6.       opacity: "1",
  7.       transition: "opacity 1s",
  8.       destroy: { opacity: "0" },
  9.     },
  10.   },
  11.   "It's better to fade out than to burn away"
  12. );
  13. ```

The all value of transition-property is not supported.

The eventlisteners module

The event listeners module gives powerful capabilities for attaching
event listeners.

You can attach a function to an event on a vnode by supplying an
object at on with a property corresponding to the name of the event
you want to listen to. The function will be called when the event
happens and will be passed the event object that belongs to it.

  1. ```mjs
  2. function clickHandler(ev) {
  3.   console.log("got clicked");
  4. }
  5. h("div", { on: { click: clickHandler } });
  6. ```

In JSX, you can use on like this:

  1. ``` js
  2. <div on={{ click: clickHandler }} />
  3. ```

Snabbdom allows swapping event handlers between renders. This happens without
actually touching the event handlers attached to the DOM.

Note, however, that you should be careful when sharing event
handlers between vnodes, because of the technique this module uses
to avoid re-binding event handlers to the DOM. (And in general,
sharing data between vnodes is not guaranteed to work, because modules
are allowed to mutate the given data).

In particular, you should not do something like this:

  1. ```mjs
  2. // Does not work
  3. const sharedHandler = {
  4.   change: function (e) {
  5.     console.log("you chose: " +;
  6.   },
  7. };
  8. h("div", [
  9.   h("input", {
  10.     props: { type: "radio", name: "test", value: "0" },
  11.     on: sharedHandler,
  12.   }),
  13.   h("input", {
  14.     props: { type: "radio", name: "test", value: "1" },
  15.     on: sharedHandler,
  16.   }),
  17.   h("input", {
  18.     props: { type: "radio", name: "test", value: "2" },
  19.     on: sharedHandler,
  20.   }),
  21. ]);
  22. ```

For many such cases, you can use array-based handlers instead (described above).
Alternatively, simply make sure each node is passed unique on values:

  1. ```mjs
  2. // Works
  3. const sharedHandler = function (e) {
  4.   console.log("you chose: " +;
  5. };
  6. h("div", [
  7.   h("input", {
  8.     props: { type: "radio", name: "test", value: "0" },
  9.     on: { change: sharedHandler },
  10.   }),
  11.   h("input", {
  12.     props: { type: "radio", name: "test", value: "1" },
  13.     on: { change: sharedHandler },
  14.   }),
  15.   h("input", {
  16.     props: { type: "radio", name: "test", value: "2" },
  17.     on: { change: sharedHandler },
  18.   }),
  19. ]);
  20. ```


SVG just works when using the h function for creating virtual
nodes. SVG elements are automatically created with the appropriate

  1. ```mjs
  2. const vnode = h("div", [
  3.   h("svg", { attrs: { width: 100, height: 100 } }, [
  4.     h("circle", {
  5.       attrs: {
  6.         cx: 50,
  7.         cy: 50,
  8.         r: 40,
  9.         stroke: "green",
  10.         "stroke-width": 4,
  11.         fill: "yellow",
  12.       },
  13.     }),
  14.   ]),
  15. ]);
  16. ```

See also the SVG example and the SVG Carousel example.

Classes in SVG Elements

Certain browsers (like IE <=11) [do not support classList property in SVG elements](
Because the _class_ module internally uses classList, it will not work in this case unless you use a classList polyfill.
(If you don't want to use a polyfill, you can use the class attribute with the _attributes_ module).


The thunk function takes a selector, a key for identifying a thunk,
a function that returns a vnode and a variable amount of state
parameters. If invoked, the render function will receive the state

thunk(selector, key, renderFn, [stateArguments])

The renderFn is invoked only if the renderFn is changed or [stateArguments] array length or it's elements are changed.

The key is optional. It should be supplied when the selector is
not unique among the thunks siblings. This ensures that the thunk is
always matched correctly when diffing.

Thunks are an optimization strategy that can be used when one is
dealing with immutable data.

Consider a simple function for creating a virtual node based on a number.

  1. ```mjs
  2. function numberView(n) {
  3.   return h("div", "Number is: " + n);
  4. }
  5. ```

The view depends only on n. This means that if n is unchanged,
then creating the virtual DOM node and patching it against the old
vnode is wasteful. To avoid the overhead we can use the thunk helper

  1. ```mjs
  2. function render(state) {
  3.   return thunk("num", numberView, [state.number]);
  4. }
  5. ```

Instead of actually invoking the numberView function this will only
place a dummy vnode in the virtual tree. When Snabbdom patches this
dummy vnode against a previous vnode, it will compare the value of
n. If n is unchanged it will simply reuse the old vnode. This
avoids recreating the number view and the diff process altogether.

The view function here is only an example. In practice thunks are only
relevant if you are rendering a complicated view that takes
significant computational time to generate.


Note that JSX fragments are still experimental and must be opted in.
See [fragment](#fragment-experimental) section for details.


Add the following options to your tsconfig.json:

  1. ``` json
  2. {
  3.   "compilerOptions": {
  4.     "jsx": "react",
  5.     "jsxFactory": "jsx",
  6.     "jsxFragmentFactory": "Fragment"
  7.   }
  8. }
  9. ```

Then make sure that you use the .tsx file extension and import the jsx function and the Fragment function at the top of the file:

  1. ```tsx
  2. import { Fragment, jsx, VNode } from "snabbdom";

  3. const node: VNode = (
  4.   <div>
  5.     <span>I was created with JSX</span>
  6.   </div>
  7. );

  8. const fragment: VNode = (
  9.   <>
  10.     <span>JSX fragments</span>
  11.     are experimentally supported
  12.   </>
  13. );
  14. ```


Add the following options to your babel configuration:

  1. ``` json
  2. {
  3.   "plugins": [
  4.     [
  5.       "@babel/plugin-transform-react-jsx",
  6.       {
  7.         "pragma": "jsx",
  8.         "pragmaFrag": "Fragment"
  9.       }
  10.     ]
  11.   ]
  12. }
  13. ```

Then import the jsx function and the Fragment function at the top of the file:

  1. ``` js
  2. import { Fragment, jsx } from "snabbdom";

  3. const node = (
  4.   <div>
  5.     <span>I was created with JSX</span>
  6.   </div>
  7. );

  8. const fragment = (
  9.   <>
  10.     <span>JSX fragments</span>
  11.     are experimentally supported
  12.   </>
  13. );
  14. ```

Virtual Node


- sel
- elm
- key

sel : String

The .sel property of a virtual node is the CSS selector passed to
[h()](#snabbdomh) during creation. For example: h('div#container', {}, [...]) will create a a virtual node which has div#container as
its .sel property.

data : Object

The .data property of a virtual node is the place to add information
for modules to access and manipulate the
real DOM element when it is created; Add styles, CSS classes,
attributes, etc.

The data object is the (optional) second parameter to [h()](#snabbdomh)

For example h('div', {props: {className: 'container'}}, [...]) will produce a virtual node with

  1. ```mjs
  2. ({
  3.   props: {
  4.     className: "container",
  5.   },
  6. });
  7. ```

as its .data object.

### children : Array

The .children property of a virtual node is the third (optional)
parameter to [h()](#snabbdomh) during creation. .children is
simply an Array of virtual nodes that should be added as children of
the parent DOM node upon creation.

For example h('div', {}, [ h('h1', {}, 'Hello, World') ]) will
create a virtual node with

  1. ```mjs
  2. [
  3.   {
  4.     sel: "h1",
  5.     data: {},
  6.     children: undefined,
  7.     text: "Hello, World",
  8.     elm: Element,
  9.     key: undefined,
  10.   },
  11. ];
  12. ```

as its .children property.

text : string

The .text property is created when a virtual node is created with
only a single child that possesses text and only requires
document.createTextNode() to be used.

For example: h('h1', {}, 'Hello') will create a virtual node with
Hello as its .text property.

elm : Element

The .elm property of a virtual node is a pointer to the real DOM
node created by snabbdom. This property is very useful to do
calculations in hooks as well as

key : string | number

The .key property is created when a key is provided inside of your
[.data](#data--object) object. The .key property is used to keep
pointers to DOM nodes that existed previously to avoid recreating them
if it is unnecessary. This is very useful for things like list
reordering. A key must be either a string or a number to allow for
proper lookup as it is stored internally as a key/value pair inside of
an object, where .key is the key and the value is the
[.elm](#elm--element) property created.

If provided, the .key property must be unique among sibling elements.

For example: h('div', {key: 1}, []) will create a virtual node
object with a .key property with the value of 1.

Structuring applications

Snabbdom is a low-level virtual DOM library. It is unopinionated with
regards to how you should structure your application.

Here are some approaches to building applications with Snabbdom.

  a repository containing several example applications that
  demonstrates an architecture that uses Snabbdom.
- Cycle.js
  "A functional and reactive JavaScript framework for cleaner code"
  uses Snabbdom
- Vue.js use a fork of snabbdom.
  redux-like architecture on top of snabbdom bindings.
- kaiju -
  Stateful components and observables on top of snabbdom
- Tweed
  An Object Oriented approach to reactive interfaces.
- Cyclow -
  "A reactive frontend framework for JavaScript"
  uses Snabbdom
- Tung
  A JavaScript library for rendering html. Tung helps to divide html and JavaScript development.
- sprotty - "A web-based diagramming framework" uses Snabbdom.
- Mark Text - "Realtime preview Markdown Editor" build on Snabbdom.
  "Tiny vdom app framework. Pure Redux. No boilerplate." - Built with :heart: on Snabbdom.
- Backbone.VDOMView - A Backbone View with VirtualDOM capability via Snabbdom.
- Rosmaro Snabbdom starter - Building user interfaces with state machines and Snabbdom.
- Pureact - "65 lines implementation of React incl Redux and hooks with only one dependency - Snabbdom"
- Snabberb - A minimalistic Ruby framework using Opal and Snabbdom for building reactive views.
- WebCell - Web Components engine based on JSX & TypeScript

Be sure to share it if you're building an application in another way
using Snabbdom.

Common errors

  1. ```text
  2. Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node':
  3.     The node before which the new node is to be inserted is not a child of this node.
  4. ```

The reason for this error is reusing of vnodes between patches (see code example), snabbdom stores actual dom nodes inside the virtual dom nodes passed to it as performance improvement, so reusing nodes between patches is not supported.

  1. ```mjs
  2. const sharedNode = h("div", {}, "Selected");
  3. const vnode1 = h("div", [
  4.   h("div", {}, ["One"]),
  5.   h("div", {}, ["Two"]),
  6.   h("div", {}, [sharedNode]),
  7. ]);
  8. const vnode2 = h("div", [
  9.   h("div", {}, ["One"]),
  10.   h("div", {}, [sharedNode]),
  11.   h("div", {}, ["Three"]),
  12. ]);
  13. patch(container, vnode1);
  14. patch(vnode1, vnode2);
  15. ```

You can fix this issue by creating a shallow copy of the object (here with object spread syntax):

  1. ```mjs
  2. const vnode2 = h("div", [
  3.   h("div", {}, ["One"]),
  4.   h("div", {}, [{ ...sharedNode }]),
  5.   h("div", {}, ["Three"]),
  6. ]);
  7. ```

Another solution would be to wrap shared vnodes in a factory function:

  1. ```mjs
  2. const sharedNode = () => h("div", {}, "Selected");
  3. const vnode1 = h("div", [
  4.   h("div", {}, ["One"]),
  5.   h("div", {}, ["Two"]),
  6.   h("div", {}, [sharedNode()]),
  7. ]);
  8. ```

Opportunity for community feedback

Pull requests that the community may care to provide feedback on should be
merged after such opportunity of a few days was provided.