gpt4 book ai didi

reactjs - 意外的命名空间导入导入/无命名空间

转载 作者:行者123 更新时间:2023-12-03 13:50:45 24 4
gpt4 key购买 nike

我正在尝试在 React 中导入 D3v4 来生成 Dash 组件。这是我到目前为止的代码:

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

};

但是当我尝试使用 $ npm run prepublish 预发布 Dash 时,它会抛出错误:

error Unexpected namespace import  import/no-namespace

它指向import * as d3 from 'd3';

我正在以 Plotly 提供的示例为基础进行构建编写您自己的组件。

最佳答案

esling Docs 中所述:

Reports if namespace import is used.

所以你应该将其更改为:

import d3 from 'd3';

或者如果您必须使用命名空间(如 eslint 文档中提到的),则禁用该规则

更新
读完docs of d3后他们使用相同的模式,所以我假设没有 d3default 导出,因此:

import d3 from 'd3';

可能不起作用。
然后对各个部分使用命名导入,如下所示:

import {scaleLinear} from "d3-scale";  

或者像我上面提到的那样禁用规则。

关于reactjs - 意外的命名空间导入导入/无命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47478970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com