A
A
Alexander Ivanov2018-02-21 17:19:37
JavaScript
Alexander Ivanov, 2018-02-21 17:19:37

Why doesn't the new syntax work?

const initState = ['test','more'];

// работает
export default function values(state = initState, action){
    if(action.type === 'ADD_VAL'){
        return [
            ...state,
            action.date
        ];
    }
    return state;
};

// не работает
export default values = (state = initState, action) => {
    if(action.type === 'ADD_VAL'){
        return [
            ...state,
            action.date
        ];
    }
    return state;
};


// тоже работает однако мне не нравится экспорт вниз переносить
// видимо без этого в данном случае никак или ?
const values = (state = initState, action) => {
    if(action.type === 'ADD_VAL'){
        return [
            ...state,
            action.date
        ];
    }
    return state;
};

export default values;

I rewrote the function to the new IDE syntax, the error does not swear at anything.
Correctly, I understand that in some cases it is necessary to use the standard. does the new syntax fail, or is it a bit deeper here?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-02-21
@cimonlebedev

export default (state = initState, action) => {
    if(action.type === 'ADD_VAL'){
        return [
            ...state,
            action.date
        ];
    }
    return state;
};

or:
const values = (state = initState, action) => {
    if(action.type === 'ADD_VAL'){
        return [
            ...state,
            action.date
        ];
    }
    return state;
};

export default values;

The second option is preferable because named functions are easier to debug.

E
Egor Zhivagin, 2018-02-21
@Krasnodar_etc

Are you doing it in a browser? Use Babel

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question