Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 how to do multiple default exports

I am still getting a hang of react+redux, and ES6. I am trying to implement socketio, and I come across the problem of having to export socketio connect with my redux's connect.

redux connect

export default connect(mapStateToProps, matchDispatchToProps)(UserList);

socketio connect

export default socketConnect(App);

Question What is the correct syntax to make them work together?

like image 516
edmamerto Avatar asked Jun 28 '17 15:06

edmamerto


People also ask

Can you have multiple export defaults?

There are two different types of export, named and default. You can have multiple named exports per module but only one default export.

Can you have multiple default exports JS?

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.

How do I export multiple default functions in React?

Use named exports to export multiple functions in React, e.g. export function A() {} and export function B() {} . The exported functions can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.

How do I export multiple variables in JavaScript?

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.


2 Answers

You can't have more than one default export.

Instead, use named exports.

// moduleName.js    

export const ConnectedUserList = connect(mapStateToProps, matchDispatchToProps)(UserList)

export const RealTimeApp = socketConnect(App);

Require the exports by name.

// otherModule.js
import { ConnectedUserList, RealTimeApp } from "./moduleName"
like image 195
AJcodez Avatar answered Sep 19 '22 05:09

AJcodez


You can mix default export and named export.

export default ConnectedUserList = connect(mapStateToProps, matchDispatchToProps)(UserList)

export const RealTimeApp = socketConnect(App);

And after, you can import your exports :

import ConnectedUserList, { RealTimeApp } from "./moduleName"
like image 40
lebol Avatar answered Sep 19 '22 05:09

lebol