Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should redux action types be in UPPER_CASE?

Tags:

redux

Is there any deeper reason why action types in redux should be strings in all upper case with underscores?

A string is just a string, right? Why not use the the names of action creators (typically in camelCase) also as the type of the actions created by them?

like image 751
travelboy Avatar asked Mar 31 '17 09:03

travelboy


Video Answer


3 Answers

capital letters for constants

It is just a convention which is common also in other other languages.

Interesting article on redux naming conventions: https://decembersoft.com/posts/a-simple-naming-convention-for-action-creators-in-redux-js/

like image 89
GibboK Avatar answered Oct 12 '22 07:10

GibboK


It is mainly because of actions is being refereed from multiple modules, be it saga or react ui. It is just the convention, and gives easy readability in code. In ReactJs, actions are important focus as they are meant to change the state of the reactUI.

like image 35
mhdnp1234 Avatar answered Oct 12 '22 07:10

mhdnp1234


It is a commonly seen pattern to use constants for mutation types in various Flux implementations. This allows the code to take advantage of tooling like linters, and putting all constants in a single file allows your collaborators to get an at-a-glance view of what mutations are possible in the entire application.

Whether to use constants is largely a preference - it can be helpful in large projects with many developers, but it's totally optional if you don't like them.

Quote from Vuex which is similar to Redux but for Vue.js only.

Since constants are generally UPPER_CASE, and if you make the string the same as the contant's name, you only need one name instead of two, which makes things a little simpler.

Overall I think it is just convention and preference. And it is optional.

like image 1
Tyler Liu Avatar answered Oct 12 '22 07:10

Tyler Liu