Muditx
Muditx

Reputation: 355

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??

Upvotes: 30

Views: 37838

Answers (3)

kirtan thummar
kirtan thummar

Reputation: 1

You can remove this warning by just adding the comment "// eslint-disable-next-line" before export.

import axios from 'axios'

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

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

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

// eslint-disable-next-line
export default {
    getAll:getAll,
    create:create
}

Upvotes: -1

Mooenz
Mooenz

Reputation: 31

You can also export the function individually to get rid of the warning:

import axios from 'axios';

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

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

Upvotes: 3

angelo
angelo

Reputation: 2951

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

const exportedObject = {
     getAll,
     create,
};

export default exportedObject;

Upvotes: 79

Related Questions