d3 prefer join over enter, exit or merge
Friday, October 30, 2020
d3 prefer join over enter, exit or merge
Since d3 v4 it has been possible to use selection.join() which aims to simplify data binding in d3.
With join you no longer need to manually call .merge, .enter or .exit on selections. You can still access enter, exit and update selections within the join function if needed.
Instructions
checkmark-circle
selection.join("rect")
error-circle
selection.enter().append("rect"), selection.exit().remove()
info-circle
It keeps code simpler and you can still access enter, update and exit selections within join function if required
Code Examples
Using join
let items = d3.select('svg')
.selectAll('rect')
.data(data);
items.join('rect');
Using enter
let selection = d3.select('svg').data(data);
let newItems = selection.enter();
newItems.append('rect');// update new items
let exitingItems = selection.exit();
Have a question or comment?