“Is assigned a value but never used” warning in Redux reducer

Here are my codes

import * as actionTypes from '../actions/actionTypes';



const counterReducer = (state = 0, action) => {
let newState;

switch (action.type) {
    case actionTypes.INCREASE_COUNTER:
        return (newState = state + action.payload);
    case actionTypes.DECREASE_COUNTER:
        return (newState = state - action.payload);
    case actionTypes.INCREASE_BY_TWO_COUNTER:
        return (newState = state + action.payload);
    default:
        return state;
 }
}
export default counterReducer;

But my terminal says ‘newState’ is assigned a value but never used. What am I missing ?

Answer

You don’t need newState variable, it’s useless in the way you are using it. You can have the same result without it. The return statement will return the result and end the execution without the need to use the variable.

import * as actionTypes from '../actions/actionTypes';



const counterReducer = (state = 0, action) => {
switch (action.type) {
    case actionTypes.INCREASE_COUNTER:
        return state + action.payload;
    case actionTypes.DECREASE_COUNTER:
        return state - action.payload;
    case actionTypes.INCREASE_BY_TWO_COUNTER:
        return state + action.payload;
    default:
        return state;
 }
}
export default counterReducer;