Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 972
Rcandy Avatar asked Jun 08 '20 09:06

Rcandy


1 Answers

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

like image 66
Kieren Hughes Avatar answered Sep 20 '22 18:09

Kieren Hughes