ecs ecs - 3 months ago 20
React JSX Question

Use Numbers For Redux Action Names

A common convention when using Redux is to store actions in their own file as

const
s like this:

export const USER_AUTH_ATTEMPT = 'USER_AUTH_ATTEMPT';
export const USER_AUTH_CHECK = 'USER_AUTH_CHECK';
export const USER_AUTH_FAIL = 'USER_AUTH_FAIL';
export const USER_AUTH_SUCCESS = 'USER_AUTH_SUCCESS';
export const USER_AUTH_LOGOUT = 'USER_AUTH_LOGOUT';


The value for the action name is usually a
string
. Would there be any advantage in using a convention like this instead?:

export const USER_AUTH_ATTEMPT = 0;
export const USER_AUTH_CHECK = 1;
export const USER_AUTH_FAIL = 2;
export const USER_AUTH_SUCCESS = 3;
export const USER_AUTH_LOGOUT = 4;


The values for the actions would be stored as integers which could reduce the file size. Would there be any negative impacts to doing this?

Answer

In the redux architecture, actions are dispatched to a store, not a single reducer. So you can have multiple reducers that react to the same action. This is also why you have a default: return state-like branch in your switch statements inside reducers.

If you do this, you have to be completely sure no two actions have the same number ID, which is harder than with strings.

The other important reason is debugging, for example with redux-logger (it would print numbers instead of helpful, descriptive action names).

Edit: Qianyue's answer gives another excellent reason not to do it.

Comments