javascript - React 组件 ESLint 表示更喜欢默认导出

标签 javascript reactjs eslint

我不断从我的组件上的 ESLint 收到此错误。

ESLint:显示“首选默认导出”(import/prefer-default-export)

这是组件的外观

export class mycomponent extends React.Component {

  render() {

    //stuff here

  }
}

它要求什么?我该如何解决这个问题?

最佳答案

您需要将导出指定为默认值,如下所示:

export default class mycomponent extends React.Component {

  render() {

    //stuff here

  }
}

(注意添加的词default),然后在其他文件中您可以使用以下方式导入组件:

从'./mycomponent.js'导入mycomponent;

假设该组件包含在同一目录中,并在文件 mycomponent.js 中定义。

如果您的文件包含多个导出的内容,其名称如下:

export const foo = 'foo';
export const bar = 'bar';

或者您甚至可以完全保留原始文件,不使用 default 一词,然后使用批量导入来导入它:

从'./mycomponent.js'导入*作为mycomponent;

关于javascript - React 组件 ESLint 表示更喜欢默认导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43442826/

相关文章:

javascript - 在 React 子组件上调用方法

javascript - 从javascript中的其他命名函数调用命名函数

reactjs - axios catch 不捕获错误

javascript - 为什么我的 ESLint 配置文件不能与 React Native 一起使用?

javascript - 在 Eslint 配置文件中禁用报价规则

javascript - 根据 SignalR 输入不断更新 d3 图表中的线条

javascript - 谷歌地图不显示

reactjs - 在react中立即setState后无法获取新值

reactjs - 功能组件的this.refs (useRef, createRef) | react native

eslint - 如何禁用有关一些未使用参数的警告,但保留 "@typescript-eslint/no-unused-vars"规则