Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign object to a variable before exporting as module default warning

    import axios from 'axios'

const baseUrl = 'http://localhost:3001/persons';

const getAll = () => {
    return axios.get(baseUrl);
}

const create = (newObject) => {
    return axios.post(baseUrl,newObject);
}

export default {
    getAll:getAll,
    create:create
}

Using axios to fetch data from server(in reactjs from json server), everything works fine but console shows this warning: Line 13:1: Assign object to a variable before exporting as module default. How to remove this warning??

like image 430
Muditx Avatar asked Jan 15 '21 15:01

Muditx


People also ask

Why would you create a default export in a module?

Default Exports: Default exports are useful to export only a single object, function, variable. During the import, we can use any name to import.

Why we use export default in react native?

It is used to export the object(variable, function, class) with the same name. It is used to export multiple objects from one file. It cannot be renamed when importing in another file, it must have the same name that was used to export it, but we can create its alias by using as operator.

How do I export multiple variables in JavaScript?

Use named exports to export multiple variables in JavaScript, e.g. export const A = 'a' and export const B = 'b' . The exported variables can be imported by using a named import as import {A, B} from './another-file. js' . You can have as many named exports as necessary in a file.

How to fix “assign object to a variable before exporting as module default”?

The “Assign object to a variable before exporting as module default” error occurs when one tries to export an anonymous object using a default export. To fix the error, assign the object to a variable and use a default export on the next line.

How to fix the error when exporting an object from a variable?

To fix the error, assign the object to a variable and use a default export on the next line. function sum(a, b) { return a + b; } const multiply = (a, b) => { return a * b; }; // anonymous default export // Assign object to a variable before exporting as module default eslint import/no-anonymous-default-export export default { sum, multiply, };

How to export a function as default object?

You need to assign the object to a variable before exporting it as default. const exportedObject = { getAll, create, }; export default exportedObject; You can also export the function individually to get rid of the warning:


Video Answer


1 Answers

You need to assign the object to a variable before exporting it as default.

const exportedObject = {
     getAll,
     create,
};

export default exportedObject;
like image 96
angelo Avatar answered Oct 07 '22 23:10

angelo