Giter Site home page Giter Site logo

missing `key` in the demonstration code about d4 HOT 2 OPEN

Pomax avatar Pomax commented on September 28, 2024
missing `key` in the demonstration code

from d4.

Comments (2)

tbranyen avatar tbranyen commented on September 28, 2024

For the particular example you chose isn't the # of elements predetermined? You should never get additions and replacements happening if you were to animate. For instance I used that code with my own virtual dom tool and found it reused all the inner nodes since they matched the previous. Does React not behave the same way?

Here's my code for comparison:

const width = 960;
const height = 500;
const voronoi = d3.voronoi().extent([[-1, -1], [width + 1, height + 1]]);

const color = (d) => {
  const dx = d[0] - width / 2;
  const dy = d[1] - height / 2;

  return d3.lab(100 - (dx * dx + dy * dy) / 5000, dx / 10, dy / 10);
};

const Mesh = () => {
  const sampler = poissonDiscSampler(width, height, 40);
  const samples = [];
  let sample; while (sample = sampler()) samples.push(sample);

  const paths = voronoi(samples)
    .polygons()
    .map((sample, i) => {
      const fill = color(sample.data).toString();

      return diff.html`<path
        d="M${sample.join('L')}Z"
        fill="${fill}"
        stroke="${fill}"
      />`
    });

  return diff.html`<svg width=${width} height=${height}>${paths}</svg>`;
};

const render = () => {
  diff.innerHTML(document.body.querySelector('main'), Mesh());
  requestAnimationFrame(render);
};

render();

from d4.

Pomax avatar Pomax commented on September 28, 2024

In your exact example it's not a true problem, but people take example code and then tweak it to get their own version, so if someone takes your example but throws in one or more calls that reorder the content (say a toggled .reverse() between the .polygon() and .map() functions) then their code may suddenly behave quite poorly compared to what it could be doing.

Generally it's a good idea to have key attributes even if you're sure the order never changes, because current you can never know for sure whether future you won't change their mind =)

from d4.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.