Skip to content

Instantly share code, notes, and snippets.

@jhewlett
Last active September 14, 2018 07:49
Show Gist options
  • Save jhewlett/acceb0e84bf043c17b66 to your computer and use it in GitHub Desktop.
Save jhewlett/acceb0e84bf043c17b66 to your computer and use it in GitHub Desktop.

Namespace your flux action types to prevent collisions:

function constants(namespace, constants) {
  return Object.freeze(
    constants.reduce((obj, constant) => {
      return {
        ...obj,
        [constant]: `${namespace}/${constant}`
      }
    }, {})
  )
}

const articleActions = constants('articles', ['FETCH', 'FETCH_SUCCESS', 'FETCH_ERROR'])
const authorActions  = constants('authors',  ['FETCH', 'FETCH_SUCCESS', 'FETCH_ERROR'])

console.log(articleActions.FETCH)   //'articles/FETCH'
console.log(authorActions.FETCH)    //'authors/FETCH'
@ennea8
Copy link

ennea8 commented Sep 21, 2016

Follow eslint rules

const constants = (namespace, constants) => Object.freeze(
    constants.reduce((obj, constant) => ({
      ...obj, [constant]: `${namespace}/${constant}`,
    }), {})
  )

@zxlwbi199101
Copy link

for eslint "no-shadow" (constants declared in upper scope)

const constants = (namespace, keys) => Object.freeze(
  keys.reduce((obj, key) => ({
    ...obj, [key]: `${namespace}/${constant}`,
  }), {});
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment