d3.event is null in a ReactJS + d3JS component

前端 未结 5 1158
北恋
北恋 2020-12-17 20:22

I\'m using ReactJS, d3JS and ES6 to create an org chart. I can create the chart and see it. But I want to add the zoom behavior to the chart, so I used

5条回答
  •  伪装坚强ぢ
    2020-12-17 21:13

    If you are importing multiple d3 packages. these packages have a d3 object accessible which might clash with one another. you have to import them with different names.

    import d3 from 'd3-selection'
    import d3Zoom from 'd3-zoom'
    import d3Scale from 'd3-scale'
    

    and have a anonymous function for your callback

    .on("zoom", () => d3.select("#viewport")
        .attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
    )
    

提交回复
热议问题