Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefer default export eslint error

I am getting this eslint error:

Prefer default export

import React, { Component } from 'react';

class HomePage extends Component {
  render() {
    return (
      <div className="Section">HomePage</div>
    );
  }
}

export { HomePage };

I have tried doing: export { default as Homepage };

and then I get a fatal parsing error.

Then I changed it to:

export default HomePage;

Which clears the eslint error.

But then throws:

'./HomePage' does not contain an export named 'HomePage'.

Because I am calling HomePage like this: import { HomePage } from './HomePage';

If I remove the brackets then I get this error:

"export 'default' (imported as 'HomePage') was not found in './HomePage'

import HomePage from './HomePage';
<PrivateRoute exact path="/" component={HomePage} />

What would be the proper way of changing this to the preferred default export?

like image 290
David Brierton Avatar asked Feb 12 '18 16:02

David Brierton


People also ask

Do I need to do export default to fix ESLint warning?

its Recommended to export atleast function per module using export default irrespective of whether the module exports only one or more than one. Regarding your question yes you need to do export default to fix EsLint warning In the rules, did you try "import/prefer-default-export": 0 ?

How to fix ESLint error when exporting image from Vue component props?

eslint error when exporting image - prefer default export 3 Array type on Vue Component Props breaks the Linter 2 Unexpected token = import/no-named-as-default eslint error 1 Prefer default export.eslint(import/prefer-default-export) for constant

What are the ESLint errors when importing styles into React component?

Eslint babel suppress error when importing styles into react component 2 React Component ESLint says Prefer Default Export 1 eslint error when exporting image - prefer default export 3 Array type on Vue Component Props breaks the Linter

When to use default export instead of named export?

When there is only a single export from a module, prefer using default export over named export. The following patterns are considered warnings: The following patterns are not warnings: // export-star.js // Any batch export will disable this rule.


3 Answers

From eslint-plugin-import

When there is only a single export from a module, prefer using default export over named export.

class HomePage extends Component {
  //....
}

export default HomePage

In another file :

import HomePage from './Hello';

Check here codesandbox

like image 142
RIYAJ KHAN Avatar answered Oct 11 '22 02:10

RIYAJ KHAN


Here's an example using functions:

function HomePage() {
    function aHelperMethod() {
        //
    }

    return {
        aHelperMethod,
    }
}

Now to import it in another file

import HomePage from './Hello';

And to use it you'll have to instantiate it

const homePage = HomePage()
homePage.aHelperFunction()
like image 33
Archmede Avatar answered Oct 11 '22 02:10

Archmede


In some cases there should be more than one named export in the module.

export const foo = 'foo';
export const bar = 'bar';
like image 1
Mahdiyeh Avatar answered Oct 11 '22 02:10

Mahdiyeh