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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Specification by Example Is Not a Test Framework
  • Monolithic First
  • Dynatrace Perform: Day Two
  • How To Build a Multi-Zone Java App in Days With Vaadin, YugabyteDB, and Heroku

Trending

  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  • Building Enterprise-Ready Landing Zones: Beyond the Initial Setup
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  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.1K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Specification by Example Is Not a Test Framework
  • Monolithic First
  • Dynatrace Perform: Day Two
  • How To Build a Multi-Zone Java App in Days With Vaadin, YugabyteDB, and Heroku

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!