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??
Default Exports: Default exports are useful to export only a single object, function, variable. During the import, we can use any name to import.
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.
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.
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.
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, };
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:
You need to assign the object to a variable before exporting it as default.
const exportedObject = {
getAll,
create,
};
export default exportedObject;
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