Skip to content
This repository has been archived by the owner on Sep 11, 2018. It is now read-only.

Any way to avoid duplicating name/value of constants? #1152

Closed
jonaswindey opened this issue Jan 25, 2017 · 3 comments
Closed

Any way to avoid duplicating name/value of constants? #1152

jonaswindey opened this issue Jan 25, 2017 · 3 comments

Comments

@jonaswindey
Copy link

In the modules we are using

export const COUNTER_INCREMENT = 'COUNTER_INCREMENT'
export const COUNTER_DOUBLE_ASYNC = 'COUNTER_DOUBLE_ASYNC'

I know that there was a createConstants helper in the past, but it is gone. Are there any ways to replace this with a more concise way? It seems redundant to me to repeat values that are the same as the variable name.

Although I don't really see an easy solution, since we export each const separately, otherwise we could just export const constants = {....} and use a helper like create-constants

@pleunv
Copy link

pleunv commented Jan 25, 2017

Not much you can do if you want a named export for each actionType.

You can either define them manually (I don't see why the redundancy is an issue as you will rarely have to touch your actionTypes once they have been defined), you can use a utility like redux-action-help which gives you the createConstants helper, or you can get rid of actionTypes altogether and use an actionCreator factory utility like the one in redux-actions.

Personally I don't mind it as I'm namespacing most of my actionTypes anyway, something like this:

export const ADD_USER = '@@accounts/user/ADD';

@piu130
Copy link

piu130 commented Feb 8, 2017

So you recommend something like this?

// ------------------------------------
// Constants
// ------------------------------------
export const PREFIX = '@@accounts/'
export const ADD_USER = `${PREFIX}user/ADD`
export const DELETE_USER = `${PREFIX}user/DELETE`
...

@@ seems to be for private actions

@dvdzkwsk
Copy link
Owner

Closing as a usage question, and not a project issue. A createConstants helper is probably the way to go, but I removed it in favor of keeping the demo code as straightforward as possible.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants