I think line 1 and 2 is meant for defining and storing the action names in variables: INCREMENT and DECREMENT. But I think you’re not utilising that. You should make use of these variables by referring to them rather than explicitly writing the strings 'INCREMENT' and 'DECREMENT'. The benefit of re-using these variables is that it’s easier to refactor the code later on, because you only have to change the value of the INCREMENT variable on one place (where the variable is declared).
Here’s an example with the difference:
const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT';
const counterReducer = (state = 0, action) => {
switch (action.type) {
case INCREMENT: // Re-use INCREMENT variable and not the 'INCREMENT' string
state++;
break;
case DECREMENT: // Re-use DECREMENT variable and not the 'DECREMENT' string
state--;
break;
}
return state;
}
const incAction = () => {
return {
type: INCREMENT // Re-use INCREMENT variable and not the 'INCREMENT' string
}
};
const decAction = () => {
return {
type: DECREMENT // Re-use DECREMENT variable and not the 'DECREMENT' string
}
};
const store = Redux.createStore(counterReducer);
Actually the main benefit of using constants for action names instead of string literals is that typos become easier to catch. If you accidentally spell a string literal ‘DECERMENT’, it will silently fall through and not change any state, whereas if you typo the constant, it will fail with an error.