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?
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 ?
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
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 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.
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
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()
In some cases there should be more than one named export in the module.
export const foo = 'foo';
export const bar = 'bar';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With