Home > Software design >  refactoring d3 js code from v5.16 to v6.6.2, d3.event breaking change
refactoring d3 js code from v5.16 to v6.6.2, d3.event breaking change

Time:12-23

New to d3 and fixing an error. I am working with a js file using the d3 library. The following line of code does not error out in v5.16:

    function br(d) {
        let s = d3.event.selection || xScaleSelector.range()
        let extent = s.map(xScaleSelector.invert, xScaleSelector);
        let window = data.filter(d => d.date >= extent[0] && d.date <= extent[1])
        update(window)
    }

However, when migrating to v6.6.2 and above, I get an error. The error I am getting is on line: let s = d3.event.selection || xScaleSelector.range() with the following error: Uncaught TypeError: Cannot read properties of undefined (reading 'selection').

The origin of the error, according to https://github.com/d3/d3/releases/tag/v6.0.0, v6.0.0 has removed the d3.event. (d3.event is undefined in v6). Can someone help suggest what changes I can make to fix this error when migrating to >= v6?

CodePudding user response:

Please try:

function br(d) {
    let s = d.selection || xScaleSelector.range()
    let extent = s.map(xScaleSelector.invert, xScaleSelector);
    let window = data.filter(d => d.date >= extent[0] && d.date <= extent[1])
    update(window)
}

Per the migration guide here

  • Related