I see that the following is fine:
const Tab = connect( mapState, mapDispatch )( Tabs );
export default Tab;
However, this is incorrect:
export default const Tab = connect( mapState, mapDispatch )( Tabs );
Yet this is fine:
export default Tab = connect( mapState, mapDispatch )( Tabs );
Can this be explained please why const
is invalid with export default
? Is it an unnecessary addition & anything declared as export default
is presumed a const
or such?
Paul's answer is the one you're looking for. However, as a practical matter, I think you may be interested in the pattern I've been using in my own React+Redux apps.
Here's a stripped-down example from one of my routes, showing how you can define your component and export it as default with a single statement:
(Note: I use the term "Scene" for the top-level component of any route).
I hope this is helpful. I think it's much cleaner-looking than the conventional
connect( mapState, mapDispatch )( BareComponent )
You can also do something like this if you want to export default a const/let, instead of
You can do something like this, which I do not like personally.
const
is likelet
, it is a LexicalDeclaration (VariableStatement, Declaration) used to define an identifier in your block.You are trying to mix this with the
default
keyword, which expects a HoistableDeclaration, ClassDeclaration or AssignmentExpression to follow it.Therefore it is a SyntaxError.
If you want to
const
something you need to provide the identifier and not usedefault
.export
by itself accepts a VariableStatement or Declaration to it's right.AFAIK the export in itself should not add anything to your current scope.
Tab
becomes an AssignmentExpression as it's given the name default ?Here
Tab = connect( mapState, mapDispatch )( Tabs );
is an AssignmentExpression.The answer shared by Paul is the best one. To expand more,
There can be only one default export per file. Whereas there can be more than one const exports. The default variable can be imported with any name, whereas const variable can be imported with any name.
At the imports side we need to import it like this:
or
With the first import, the const variable is imported whereas, with the second one, the default one will be imported.
If the component name is explained in the file name
MyComponent.js
, just don't name the component, keeps code slim.