DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Bill You Didn't See Coming
  • FinOps for Engineers: Turning Cloud Bills Into Runtime Signals
  • Cost Is a Distributed Systems Bug
  • ITSM Uncovered: How IT Teams Keep Businesses Running Smoothly

Trending

  • LLM-Powered Deep Parsing for Industrial Inventory Search
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Stop Debugging Glue Jobs Manually: Building an Agentic Observability Layer for Data Pipelines
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  1. DZone
  2. Coding
  3. JavaScript
  4. Namespacing Redux Action Type Constant Values

Namespacing Redux Action Type Constant Values

Simple constant names with namespaced values will give you clarity and reduced verbosity, while ensuring the right cases fire in your reducers.

By 
Cliff Hall user avatar
Cliff Hall
·
Aug. 01, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
8.2K Views

Join the DZone community and get the full member experience.

Join For Free

Most everyone agrees that defining constants for your Redux action types is a good idea. If you use string literals, it’s all too easy to misspell one and wonder why your reducer isn’t responding. If you use a constant, your IDE can point out that gaffe right away.

Here’s an example from the Redux documentation:

export const ADD_TODO = 'ADD_TODO'
export const TOGGLE_TODO = 'TOGGLE_TODO'
export const SET_VISIBILITY_FILTER = 'SET_VISIBILITY_FILTER'

Since it’s in the docs, this is how most projects you see implement them. So, you’re asking, is there any reason to do it differently? Yes, there is.

In any non-trivial app, you’ll probably define action type constants and the action creators that use them in several different files, separated by functional areas. Different team members may be adding to them all the time, so an action type constant in one file conflicting with one in another file is always a possibility.

For instance, consider the constants defined these two files:

articles.actions.js

export const FETCH_REQUESTED = 'FETCH_REQUESTED'
export const FETCH_COMPLETE  = 'FETCH_COMPLETE'
export const FETCH_ERROR     = 'FETCH_ERROR'

profiles.actions.js

export const FETCH_REQUESTED = 'FETCH_REQUESTED'
export const FETCH_COMPLETE  = 'FETCH_COMPLETE'
export const FETCH_ERROR     = 'FETCH_ERROR'

If your action types are defined in separate files, chances are the reducers that respond to them are as well. When you combine your reducers, you essentially create one big switch statement, and if you have two action types with the same constant value, the wrong case block will eventually be fired for one of them.

Is the answer to make the constant names and values longer?

export const FETCH_PROFILES_REQUESTED = 'FETCH_PROFILES_REQUESTED'
export const FETCH_PROFILES_COMPLETE = 'FETCH_PROFILES_COMPLETE'
export const FETCH_PROFILES_ERROR    = 'FETCH_PROFILES_ERROR'

Possibly, but it’s overly verbose and unnecessary.

Remember, the constant name and value need not be the same. Here’s a good way to namespace your constant values while keeping the names simple and clear within the context of their use.

articles.actions.js

export const FETCH_REQUESTED = 'articles/fetch-requested'
export const FETCH_COMPLETE  = 'articles/fetch-complete'
export const FETCH_ERROR     = 'articles/fetch-error'

profiles.actions.js

export const FETCH_REQUESTED = 'profiles/fetch-requested'
export const FETCH_COMPLETE  = 'profiles/fetch-complete'
export const FETCH_ERROR     = 'profiles/fetch-error'

In the articles reducer, we know we’re dealing with articles, so all the constants don’t need to have ARTICLES as part of the name. Same with the profiles reducer.

articles.reducer.js

import { FETCH_REQUESTED, FETCH_COMPLETE, FETCH_ERROR } from '../actions/articles.actions'

profiles.reducer.js

import { FETCH_REQUESTED, FETCH_COMPLETE, FETCH_ERROR } from '../actions/profiles.actions'

That’s it. Simple constant names with namespaced values will give you clarity and reduced verbosity, while ensuring the right cases fire in your reducers.

IT app Creator (software) Strings teams Blocks Documentation Profile (engineering) Clear (Unix)

Published at DZone with permission of Cliff Hall. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Bill You Didn't See Coming
  • FinOps for Engineers: Turning Cloud Bills Into Runtime Signals
  • Cost Is a Distributed Systems Bug
  • ITSM Uncovered: How IT Teams Keep Businesses Running Smoothly

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook