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'
@imxinlei
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