Karan Hudia
Karan Hudia

Reputation: 573

Exporting a function declared as constant in ES6

I followed a guide to implement REDUX in my react-native app. I am trying to implement actions but my eslintkeeps on giving this error on 8th line-

[eslint] Prefer default export. (import/prefer-default-export)

My code is -

import * as types from './types';

const incrementCounter = counterValue => ({
  type: types.INCREMENT_COUNTER,
  counterValue,
});

export { incrementCounter };

My question is what is the proper way to export this constant function in ES6 ?

Upvotes: 0

Views: 1935

Answers (3)

Bergi
Bergi

Reputation: 664620

The simplest change would be to add as default to your export { incrementCounter };. However, to export your function as the default export you'd rather write

import * as types from './types';

export default counterValue => ({
  type: types.INCREMENT_COUNTER,
  counterValue,
});

or

import * as types from './types';

export default function incrementCounter(counterValue) {
  return {
    type: types.INCREMENT_COUNTER,
    counterValue,
  };
}

Upvotes: 2

Hitmands
Hitmands

Reputation: 14179

import/prefer-default-export is a questionable rule, using default exports you will lose type consistence and your IDE won't be longer able to help you with refactoring, inspection and code completion.

you will be always able to import with a different name using import aliases: import {incrementCounter as foo} from 'incrementCounter'

It may appear as a personal opinion, but, I strongly suggest you to keep named exports and edit your .eslintrc:

{
  "rules": {
    "import/prefer-default-export" : 0
  }
}

Please, refer to this discussion: https://github.com/airbnb/javascript/issues/1365

Upvotes: 1

Jigar Shah
Jigar Shah

Reputation: 6223

In config.js

// Declaration of const 

const config = {
    name: 'test'
};

export default config

In another file

// Using const 

import * as config from '../config';

let name = config.name;

Upvotes: 1

Related Questions