简体   繁体   English

React Component ESLint表示“首选默认导出”

[英]React Component ESLint says Prefer Default Export

I keep getting this error from ESLint on my component. 我不断从组件上的ESLint收到此错误。

ESLint: says Prefer Default Export (import/prefer-default-export) ESLint:说“首选默认导出”(import / prefer-default-export)

Here's is how the component looks 这是组件的外观

export class mycomponent extends React.Component {

  render() {

    //stuff here

  }
}

What is it asking for? 要求什么? How can I fix this? 我怎样才能解决这个问题?

you need to specify your export as default like this: 您需要将导出指定为默认值,如下所示:

export default class mycomponent extends React.Component {

  render() {

    //stuff here

  }
}

(notice the added word default ) and then in other files you may import your component with: (注意添加的单词default ),然后在其他文件中,您可以使用以下命令导入组件:

import mycomponent from './mycomponent.js';

assuming that the component is being included from within the same directory and is defined in the file mycomponent.js. 假设该组件是从同一目录中包含的,并且在文件mycomponent.js中定义。

You can also avoid having a default export if your file contains multiple exported things with names such as: 如果文件包含多个具有以下名称的导出内容,则还可以避免默认导出:

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

or you could even leave your original file exactly as it is without the word default and import it using a batch import: 或者您甚至可以完全保留原始文件,不带default一词,然后使用批量导入将其导入:

import * as mycomponent from './mycomponent.js';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM