Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React: export const + export default vs export default

Tags:

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.

like image 433
volna Avatar asked Feb 20 '17 00:02

volna


People also ask

What is difference between export and export default React?

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.

When should I use export and export default?

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.

What does export const mean?

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.

What does export default do in React?

Default export is used to export a single class, primitive, or function from a module.


1 Answers

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.

like image 66
Andy_D Avatar answered Sep 21 '22 13:09

Andy_D