Unexpected namespace import import/no-namespace

Viewed 1860

I'm trying to import D3v4 in React to produce a Dash component. This is my code so far:

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import * as d3 from 'd3';

function createBox(divId) {
    var svgContainer = d3.select(divId).append('svg')
                                     .attr('width', 200)
                                     .attr('height', 200);

 //Draw the Circle
   svgContainer.append('circle')
                        .attr('cx', 30)
                        .attr('cy', 30)
                        .attr('r', 20);
}

/**
 * ExampleComponent is an example component.
 * It takes a property, `label`, and
 * displays it.
 * It renders an input with the property `value`
 * which is editable by the user.
 */
export default class ExampleComponent extends Component {
    constructor() {
        super();
        this.plot = this.plot.bind(this);
    }
    plot(props) {
       createBox(props.id);
    }

    componentDidMount() {
        this.plot(this.props);
    }

    componentWillReceiveProps(newProps) {
        this.plot(newProps);
    }

    render() {
        const {id} = this.props;
        return (
            <div id={id}/>
        );
    }
}

ExampleComponent.propTypes = {
    /**
     * The ID used to identify this compnent in Dash callbacks
     */
    id: PropTypes.string

};

But when I try to prepublish Dash using $ npm run prepublish it throws the error:

error Unexpected namespace import  import/no-namespace

And it points back to import * as d3 from 'd3';

I am building off of the example provided by Plotly on writing your own components.

1 Answers

As mentioned in the esling Docs:

Reports if namespace import is used.

So you should change it to this:

import d3 from 'd3';

Or disable the rule if you must use the namespace (like mentioned in the eslint docs)

Update
After reading the docs of d3 they use the same pattern so i assume there is no default export for d3 hence this:

import d3 from 'd3';

might not work.
then either use a named import for individuals parts like this:

import {scaleLinear} from "d3-scale";  

Or disable the rule like i mentioned above.

Related