Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node not re-ordered when moving upwards #404

Open
OliverJAsh opened this issue Aug 1, 2016 · 1 comment
Open

Node not re-ordered when moving upwards #404

OliverJAsh opened this issue Aug 1, 2016 · 1 comment

Comments

@OliverJAsh
Copy link

OliverJAsh commented Aug 1, 2016

I have a node with a key which is re-used and re-ordered in the DOM when it is moved downwards, but not when it is moved upwards. Is there any reason for this?

Demo

untitled

Code

var h = require('virtual-dom/h');
var diff = require('virtual-dom/diff');
var patch = require('virtual-dom/patch');
var createElement = require('virtual-dom/create-element');

// 1: Create a function that declares what the DOM should look like
function render(count)  {
    const x = [h('li', { key: 'foo' }, h('input')), h('li', { key: 'bar' }, h('input'))];
    const children = count % 2 === 0 ? x : x.reverse();
    return h('ul', children);
}

// 2: Initialise the document
var count = 0;      // We need some app data. Here we just store a count.

var tree = render(count);               // We need an initial tree
var rootNode = createElement(tree);     // Create an initial root DOM node ...
document.body.appendChild(rootNode);    // ... and it should be in the document

// 3: Wire up the update logic
setInterval(function () {
      count++;

      var newTree = render(count);
      var patches = diff(tree, newTree);
      rootNode = patch(rootNode, patches);
      tree = newTree;
}, 2000);
@OliverJAsh
Copy link
Author

Any ideas on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant