From 136a9a8bfa5ae6f5b6e60106d4517c9be25304f2 Mon Sep 17 00:00:00 2001 From: Eric Gjertsen Date: Sat, 7 Nov 2015 20:04:41 -0500 Subject: [PATCH] add warning to readme about sharing eventlistener data between VNodes --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index 8f903f5..d4c4933 100644 --- a/README.md +++ b/README.md @@ -351,6 +351,44 @@ h('div', [ 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: + +```javascript +// Does not work +var sharedHandler = { + change: function(e){ console.log('you chose: ' + e.target.value); } +}; +h('div', [ + h('input', {props: {type: 'radio', name: 'test', value: '0'}, + on: sharedHandler}), + h('input', {props: {type: 'radio', name: 'test', value: '1'}, + on: sharedHandler}), + h('input', {props: {type: 'radio', name: 'test', value: '2'}, + on: sharedHandler}) +]); +``` + +For many such cases, you can use array-based handlers instead (described above). +Alternatively, simply make sure each node is passed unique `on` values: + +```javascript +// Works +var sharedHandler = function(e){ console.log('you chose: ' + e.target.value); }; +h('div', [ + h('input', {props: {type: 'radio', name: 'test', value: '0'}, + on: {change: sharedHandler}}), + h('input', {props: {type: 'radio', name: 'test', value: '1'}, + on: {change: sharedHandler}}), + h('input', {props: {type: 'radio', name: 'test', value: '2'}, + on: {change: sharedHandler}}) +]); +``` + ## Helpers ### Thunks