12

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;
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Rcandy
  • 123
  • 1
  • 1
  • 6

2 Answers2

24

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 :).

Kieren Hughes
  • 574
  • 3
  • 8
  • 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!). – MikeBeaton Dec 16 '22 at 09:46
2

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
Ali Raza
  • 1,026
  • 13
  • 20