Properties can only be set. Not removed. Even though browsers allow addition and
@ -385,7 +385,7 @@ instead. Perhaps via [the dataset module](#the-dataset-module).
Same as props, but set attributes instead of properties on DOM elements.
```mjs
h('a', { attrs: { href: '/foo' } }, 'Go to Foo');
h('a', { attrs: { href: '/foo' } }, 'Go to Foo')
```
Attributes are added and updated using `setAttribute`. In case of an
@ -408,7 +408,7 @@ the DOM element.
Allows you to set custom data attributes (`data-*`) on DOM elements. These can then be accessed with the [HTMLElement.dataset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset) property.
This makes it easy to declaratively animate the entry of elements.
@ -470,7 +470,7 @@ h('span', {
transition: 'opacity 1s',
remove: { opacity: '0' }
}
}, 'It\'s better to fade out than to burn away');
}, 'It\'s better to fade out than to burn away')
```
This makes it easy to declaratively animate the removal of elements.
@ -484,7 +484,7 @@ h('span', {
transition: 'opacity 1s',
destroy: { opacity: '0' }
}
}, 'It\'s better to fade out than to burn away');
}, 'It\'s better to fade out than to burn away')
```
### Eventlisteners module
@ -499,9 +499,9 @@ happens and will be passed the event object that belongs to it.
```mjs
function clickHandler (ev) {
console.log('got clicked');
console.log('got clicked')
}
h('div', { on: { click: clickHandler } });
h('div', { on: { click: clickHandler } })
```
Very often, however, you're not really interested in the event object
@ -519,13 +519,13 @@ with the value in the second element once the event occurs.
```mjs
function clickHandler (number) {
console.log('button ' + number + ' was clicked!');
console.log('button ' + number + ' was clicked!')
}
h('div', [
h('a', { on: { click: [clickHandler, 1] } }),
h('a', { on: { click: [clickHandler, 2] } }),
h('a', { on: { click: [clickHandler, 3] } }),
]);
])
```
Each handler is called not only with the given arguments but also with the current event and vnode appended to the argument list. It also supports using multiple listeners per event by specifying an array of handlers:
See also the [SVG example](./examples/svg) and the [SVG Carousel example](./examples/carousel-svg/).
@ -641,7 +641,7 @@ Consider a simple function for creating a virtual node based on a number.
```mjs
function numberView (n) {
return h('div', 'Number is: ' + n);
return h('div', 'Number is: ' + n)
}
```
@ -652,7 +652,7 @@ function.
```mjs
function render (state) {
return thunk('num', numberView, [state.number]);
return thunk('num', numberView, [state.number])
}
```
@ -809,19 +809,19 @@ Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node':
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.
```mjs
var sharedNode = h('div', {}, 'Selected');
var sharedNode = h('div', {}, 'Selected')
var vnode1 = h('div', [
h('div', {}, ['One']),
h('div', {}, ['Two']),
h('div', {}, [sharedNode]),
]);
])
var vnode2 = h('div', [
h('div', {}, ['One']),
h('div', {}, [sharedNode]),
h('div', {}, ['Three']),
]);
patch(container, vnode1);
patch(vnode1, vnode2);
])
patch(container, vnode1)
patch(vnode1, vnode2)
```
You can fix this issue by creating a shallow copy of the object (here with object spread syntax):
@ -831,18 +831,18 @@ var vnode2 = h('div', [
h('div', {}, ['One']),
h('div', {}, [{ ...sharedNode }]),
h('div', {}, ['Three']),
]);
])
```
Another solution would be to wrap shared vnodes in a factory function: