React Redux error: default parameters should be last default-param-last
Asked Answered
A

2

13

Using Redux in React I'm having a warning that the default parameters should be last default-param-last. on the line where I created the const warehouse. is there wrong on how I created the code? or is there a better code for that?

Here's my codes

import {SAVE_ACTION} from '../actions/save-action';


cons initialState = {
    datasToSave:[]
};

const warehouse = (state = initialState, {type, payload}) => {
    switch(type) {
        case SAVE_ACTION: {
            const {datasToSave} = payload
            return {
                ...state,
                dataToSave
                };
            }

        default:
            return state;
    }
};

export default warehouse;
Algeciras answered 8/6, 2020 at 9:49 Comment(2)
can you add the part with the dispath (action)Adalard
Please do not vandalize your posts.Ferocious
O
26

Add a default for your action parameter:

import {SAVE_ACTION} from '../actions/save-action';


cons initialState = {
    datasToSave:[]
};

const warehouse = (state = initialState, {type, payload} = {}) => {
    switch(type) {
        case SAVE_ACTION: {
            const {datasToSave} = payload
            return {
                ...state,
                dataToSave
                };
            }

        default:
            return state;
    }
};

export default warehouse;

There's nothing wrong with your code as such. The default-param-last eslint rule just means that in this case you need to specify a default on both params or disable the rule for that particular line. The rule wants you to do ({type, payload}, state = initialState) => but that won't work as redux will call your reducer with the parameters in a different order. I would set the default on the action parameter to fix this :).

Oxidate answered 8/6, 2020 at 9:54 Comment(1)
It might be worth noting that default parameters don't have to be last in JavaScript (unlike some other languages, C# say). Any parameter which is passed in as undefined gets replaced with the default if there is one - so it's perfectly possible to call this reducer in such a way that the default for the first parameter gets used, even though the second parameter always has a defined value (it better be, as it's the default state for this part of the Redux store!).Mossberg
A
3

I completely agree with above answer but you can also disable checking for default parameter last in .eslint file.

in .eslint file add/edit:

"default-param-last": 0
Accipiter answered 5/3, 2022 at 12:24 Comment(1)
This unfortunately makes more sense as you might not want to comment dozens of reducers.Lubberly

© 2022 - 2025 — McMap. All rights reserved.