railsRabbit
railsRabbit

Reputation: 153

Can't import d3-queue in React.js? Attempted import error: 'queue' is not exported from 'd3' (imported as 'd3')

Trying to get an example working from https://bl.ocks.org/mbostock/4657115 with React.js.

I get the error: Attempted import error: 'queue' is not exported from 'd3' (imported as 'd3').

However after installing d3-queue I tried the following imports in my component:

import * as d3queue from 'd3-queue';

import {queue} from 'd3-queue';

But neither resolve the error.

What am I missing? Or is there a deprecation that I have missed?

My code:

import React from 'react';

import * as d3 from 'd3';
import * as d3queue from 'd3-queue';
import * as topojson from 'topojson';

export default class CongressionalDistricts extends React.Component {
  state = {
    usData: null,
    usCongress: null
  }

  componentWillMount() {
    d3.queue()
      .defer(d3.json, "us.json")
      .defer(d3.json, "us_congress_113.json")
      .await((error, usData, usCongress) => {
        this.setState({
          usData,
          usCongress
        });
      })
  }

  componentDidUpdate() {
    const svg = d3.select(this.refs.anchor),
                {width, height} = this.props;

    const projection = d3.geoAlbers()
                      .scale(1280)
                      .translate([width / 2, height / 2]);

    const path = d3.geoPath(projection);

    const us= this.state.usData,
          congress = this.state.usCongress

    svg.append("defs").append("path")
      .attr("id", "land")
      .datum(topojson.feature(us, us.objects.land))
      .attr("d", path);

    svg.append("clipPath")
        .attr("id", "clip-land")
      .append("use")
        .attr("xlink:href", "#land");

    svg.append("g")
        .attr("class", "districts")
        .attr("clip-path", "url(#clip-land)")
      .selectAll("path")
        .data(topojson.feature(congress, congress.objects.districts).features)
      .enter().append("path")
        .attr("d", path)
      .append("title")
        .text(function(d) { return d.id; });

    svg.append("path")
        .attr("class", "district-boundaries")
        .datum(topojson.mesh(congress, congress.objects.districts, function(a, b) { return a !== b && (a.id / 1000 | 0) === (b.id / 1000 | 0); }))
        .attr("d", path);

    svg.append("path")
        .attr("class", "state-boundaries")
        .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
        .attr("d", path);
  }

  render() {
    const { usData, usCongress } = this.state;

    if (!usData || !usCongress) {
      return null;
    }

    return <g ref="anchor" />
  }
}

Upvotes: 1

Views: 3428

Answers (2)

Yuna Luzi
Yuna Luzi

Reputation: 338

If you're using D3.v5 you will get the same error when trying to utilize d3.queue() because it was replaced by Promise.all().

Here is an example of how to use it:

Promise.all([
        d3.csv('data1.csv'),
        d3.csv('data2.csv')
        ]).then( ([data1, data2]) => {
            // do stuff
            console.log(data1, data2);
        }).catch(err => console.log('Error loading or parsing data.'))

You can find out which version of D3 you're using by running this on your terminal: npm list d3.

Upvotes: 5

Kenzk447
Kenzk447

Reputation: 525

Have you try using require instead of import?

const { queue } = require("d3-queue");

Upvotes: 0

Related Questions