Karim
Karim

Reputation: 410

Unexpected default export of anonymous function import/no-anonymous-default-export

How to avoid this kind of warning?
It gives warning like below.

 Unexpected default export of anonymous function import/no-anonymous-default-exp 

This is the function which gives warning.

import { FETCH_USER } from '../actions/types';

export default function(state = null, action){
    switch(action.type) {
        case FETCH_USER:
            return action.payload || false ;
        default:
            return state;
    }
}

Upvotes: 14

Views: 19972

Answers (3)

apubhadaram
apubhadaram

Reputation: 31

What kind of linter are you using? Linters generally don't like default exporting anonymous functions. In eslint, you can disable this on a file by adding this on top of the file:
/* eslint-disable import/no-anonymous-default-export */

if you want to disable it on a single line, add this comment on top of that line:
// eslint-disable-next-line import/no-anonymous-default-export

There are no convention with regards to this type of syntax. My tip is to just use put all your exports on the bottom of your file in order to at least have a common structure in your own projects. Just like what the others said, you can get rid of this warning by doing:

import { FETCH_USER } from '../actions/types';

const funcName = function(state = null, action){
    switch(action.type) {
        case FETCH_USER:
            return action.payload || false ;
        default:
            return state;
    }
}

export default funcName;

Upvotes: 3

Shasa
Shasa

Reputation: 81

You are getting the error because in this line of code, you have not provided the function name:

export default function(state = null, action){

The code will still work, however to get rid of the warning provide a function name like so:

export default function functionName(state = null, action){

Upvotes: 7

meirellesMS
meirellesMS

Reputation: 346

To avoid this kind of warning, now I am no longer using anonymous functions. So, try this...

export default function foo(state = null, action){
    switch(action.type) {
        case FETCH_USER:
            return action.payload || false ;
        default:
            return state;
    }
}

Upvotes: 30

Related Questions