reactjsreduxreact-reduxreducers

React Redux error: default parameters should be last default-param-last


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;

Solution

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