Redux will be dispatching actions for state change. What are naming conventions of action types in redux?
Naming Complex Action Creators Complex action creators (created by redux-thunk, redux-promise, etc) are more likely to be the agents of change in your system -- i.e. they affect your state. These should be named with present-tense verbs. For example, loadPosts() is a thunk that changes state.
What Is a Naming Convention? In simple terms, a naming convention refers to a framework used for naming your files in a specific way. This should be descriptive and consistent throughout the organization. It is always best to use a naming convention to describe the contents of the files.
Event handlers must have a name with the suffix EventHandler. The following example illustrates names that consist of the delegate name and the EventHandler suffix. Event handler naming conventions apply to pre and post event handlers.
What is the Redux Pattern? Redux is a pattern and library for managing and updating application state, using events called "actions". - Redux Documentation. Not only is redux great for defining events, it also guides the flow of events through predictable event tracking.
There are a few conventions around the community, I'll list the ones I know of and think are useful here:
The most common convention is to keep the action types ("event types") in CONSTANT_CASE
.
This avoids spelling errors, where the action has a type of my_type
, but the reducer expects a type of my-type
or My_Type
.
Another very common convention is to save the action types in a separate file as constants, e.g. var MY_ACTION_TYPE = 'MY_ACTION_TYPE';
, and to use them from there.
This also avoids spelling errors, so you don't expect an action to have a type of MY_ACTION_TYP
. If the variable doesn't exist, you'll get an error immediately, especially if you're linting.
A not quite as common, but imho very useful, convention is to scope the actions to a project and a domain. This approach was popularized by Erik Rasmussen in his "Ducks" proposal, which specifies that action types have to be in this shape: var MY_ACTION_TYPE = 'appname/domain/MY_ACTIONTYPE'
.
This avoids the case of two action constants having the same value. E.g. imagine you have a admin area and user facing area, and both have forms dispatching a 'CHANGE_USERNAME'
action type. This will make two reducers pick up the same action, where one shouldn't pick the other one up. This can happen on accident, and is very annoying to track down. By prefixing them with the app- and domain name, one avoids this issue: 'appname/admin/CHANGE_USERNAME'
is different from 'appname/user/CHANGE_USERNAME'
!
That's all the conventions I know of and use, but I'm sure somebody else has more – what have you used and found useful in your projects?
There are also some conventions around naming asynchronous action types. If you have a set of actions to represent an api call to get a user, you can split them up into something like:
FETCH_USER_REQUEST
- for when you first send the api callFETCH_USER_SUCCESS
- for when the api call is done and successfully returned dataFETCH_USER_FAIL
- for when the api call failed and responded with an error,FETCH_USER_COMPLETE
- sometimes used at the end of the call regardless of statusIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With