Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get rid of the warning import/no-anonymous-default-export in React?

I get the warning in the consle:"Assign object to a variable before exporting as module default import/no-anonymous-default-export."

This is coming from my .js functions which export several functions as default. I am not sure how to get rid of, while still being able to export several functions as default and keep the code simple as per below.

function init() {}

function log(error) {
  console.error(error);
}

export default {
  init,
  log,
};
 

I could write the file as:

export default function init() {}

export function log(error) {
  console.error(error);
}

Is there a setting I need to change, something I can do about it?

like image 916
YoungDad Avatar asked Nov 07 '20 15:11

YoungDad


People also ask

Does not contain a default export imported as react?

The "does not contain a default export" error occurs when we try to use a default import to import from a module that doesn't have a default export. To solve the error, make sure the module has a named export and wrap the import in curly braces, e.g. import {myFunction} from './myModule .

What does export default mean react?

export default is used to export a single class, function or primitive from a script file. The export can also be written as export default class HelloWorld extends React.Component { render() { return <p>Hello, world!</

How do I make export default in react?

Usually, the default export is written after the function, like the example below. Copy # react function App() { return ( <div className="App"> <header className="App-header"> <img src={logo} className="App-logo" alt="logo" /> </header> </div> ); } export default App; But it can also be written like below.

What is the difference between export default and export in react?

Exports without a default tag are Named exports. Exports with the default tag are Default exports. Using one over the other can have effects on your code readability, file structure, and component organization. Named and Default exports are not React-centric ideas.

How do I fix the “unexpected default export of anonymous function”?

The "Unexpected default export of anonymous function" warning is caused when we try to export an anonymous function using a default export. To solve the error, give the function a name before exporting it. Here is an example of how the warning is caused. The issue here is that we're using a default export to export an anonymous function.

How do I disable an anonymous default export in ESLint?

By default, the eslint rule warns us for all types of anonymous default exports, e.g. arrays, functions, classes, objects, etc. If you want to disable the rule for a single line, you can use a comment. The comment should be placed right above the code with the anonymous default export.

What is import/no-anonymous-default-exp?

Unexpected default export of anonymous function import/no-anonymous-default-exp This is the function which gives warning. import { Stack Overflow About Products For Teams

Should I give a name to the default export?

@Velidan The only issue it could cause is with maintainability. A code's logic, if implemented properly, will work regardless of whether a name is given to the default export. Putting a default export into a variable beforehand will force you to give it a name, giving consumers of the module a convention of what you recommend the import be called.


Video Answer


2 Answers

It's telling you to give the object being exported a name before exporting, to make it more likely that the name used is consistent throughout the codebase. For example, change to:

function init() {}

function log(error) {
  console.error(error);
}

const logger = {
  init,
  log,
};
export default logger;

(Give it whatever name makes most sense in context - logger is just an example)

But if you're going to export multiple things, using named exports would make a bit more sense IMO. Another option is to do:

export function init() {}

export function log(error) {
  console.error(error);
}

and then import them as:

import { init, log } from './foo.js';
like image 173
CertainPerformance Avatar answered Oct 28 '22 03:10

CertainPerformance


Asain your data in a variable, then export as default.

//Example

const data = [
  {
    id: 1,
    name: 'Bertie Yates',
    age: 29,
    image:
      'https://res.cloudinary.com/diqqf3eq2/image/upload/v1595959131/person-2_ipcjws.jpg',
  },

  {
    id: 3,
    name: 'Larry Little',
    age: 36,
    image:
      'https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883423/person-4_t9nxjt.jpg',
  },
  {
    id: 4,
    name: 'Sean Walsh',
    age: 34,
    image:
      'https://res.cloudinary.com/diqqf3eq2/image/upload/v1586883417/person-3_ipa0mj.jpg',
  },
]
export default data
like image 20
Mohammad amir Avatar answered Oct 28 '22 03:10

Mohammad amir