I've come across current component creation with "double" export. Can you please explain if there is a real use of it, or it's just author preference?
import React from 'react'
import DuckImage from '../assets/Duck.jpg'
import './HomeView.scss'
export const HomeView = () => (
<div>
<h4>Welcome!</h4>
<img
alt='This is a duck, because Redux!'
className='duck'
src={DuckImage} />
</div>
)
export default HomeView
P.S: Current code is later bundled by webpack2.
Exports without a default tag are Named exports. Exports with the default tag are Default exports. Using one over the other can have effects on your code readability, file structure, and component organization. Named and Default exports are not React-centric ideas.
The export statement is used when creating JavaScript modules to export objects, functions, variables from the module so they can be used by other programs with the help of the import statements. There are two types of exports. One is Named Exports and other is Default Exports.
export const is a named export that exports a const declaration or declarations. To emphasize: what matters here is the export keyword as const is used to declare a const declaration or declarations. export may also be applied to other declarations such as class or function declarations.
Default export is used to export a single class, primitive, or function from a module.
In this case, the two exports are exporting the same thing. Both
import Homeview
And
import { Homeview }
Will give you the same module (the HomeView component).
I see you're using Redux, though. If you were doing something like
export const HomeView ...
export default connect(mapStateToProps)(HomeView);
That could be useful in that you might want to use the non-Redux-connected component sometimes, or you might need it for testing.
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