d3/d3-force

d3 v7 chained methods of forceLink "is not a fucntion"

staysh opened this issue Β· 2 comments

staysh commented

d3.forceLink().id().strength() results in an uncaught TypeError that it is not a function

d3.forceLink().id() //ok
d3.forceLink().strength() //ok

Fil commented

works as expected; see https://github.com/d3/d3-force#link_id for details

I think the confusion here is that you can only chain when you pass link.id an argument. If you don’t pass an argument, then link.id is a getter, returning the current id. So:

const link = d3.forceLink();
console.log(link.id()); // returns the current id accessor
link.id((d) => d.id); // sets the id accessor and returns link

So, this is fine:

link.id((d) => d.id).strength(10); // πŸ‘

But this is nonsensical because link.id() without an argument returns the current id accessor, not the link force.

link.id().strength(); // πŸ‘Ž