There is a line in my project code that goes like this,
const variableName = { ... }
export default variableName
Is the variableName
written correctly in the first place? Shouldn't its be either a
export const variableName = { ... } // import { variableName } from './file'
OR
export default variableName = { ... } // import variableName from './file'
What is the right way to import this in another file?
import { variableName } from 'file'
OR
import variableName from './file'
PS - I've looked at other answers, blogs as well but they do not talk about exporting a const variable as a default.
These are all correct ways to export.
if it is exported as default:
import myComponent from '...'
if it is a named export (export x;
)
import {x} from '...';
// OR
import {x as y} from '...';
console.log(y);
export const x = /* ... */;
is just a shorthand for
const x = /* ... */;
export x;
this shorthand is not available for default export, you can't write:
export default const x = {};