NGRX Actions
Published on November 23, 2023
To study api’s it’s best to code it yourself, so let’s go over the proper way to create actions using NGRX.
This lesson has 2 student execises, below each exercise you will find the solution for that exercise.
What are actions?
- Actions are sent to the store using
store.dispatch(someAction)
- The store will send that actions to the reducers which often will result in a state change.
The meaning of an action is to say that something happened in the app, a lot of the times it’s something that the user did like clicking a button or activating some sort of event.
An action will often send additional data, for example if there is an action we want to send after a server response, we will often send the server response as the payload for the action.
Action contains an identifier and optional payload data { type: string, additionalData?: ..., moreData?: ... }
.
createAction
There are 2 creator functions in @ngrx/store
for actions createAction
and createActionGroup
.
Let’s start with createAction
which is used to create a single action.
Notice that our action contains a unique identifier, it’s common to group actions together and name the actions with square brackets and the name of the group (in the above example [GroupName]
).
You can also add any optional data that you want with your action.
Student Exercise - createAction
Time for you to practice the createAction
api.
- You can write code in the editor here in the site (notice that the editor starts with an error cause the code is not completed).
- This angular app contains a single component
app.component.ts
which will display a list of repositories from github. - The app contains
actions.ts
where you will need to create a single action - The app contains
reducer.ts
which responds to the action you will create. - The file
repos.service.ts
contains a service that will fetch the repositories from github. - The files you will modify in the exercise are:
app.component.ts
,actions.ts
andreducer.ts
(maybe), the rest of the files will be read only files. - You goal is that the
app.component.ts
will display the list of repositories from github, but grab that list from the store. - The
app.component.ts
willimplement OnInit
and in thengOnInit
method you will need to use therepos.service.ts
to grab the repos and then populate the ngrx state with those repos. - You will need to create an action in
actions.ts
that will be dispatched from theapp.component.ts
and will be handled by the reducer inreducer.ts
.
Solution
Here is the solution for the exercise:
- In the
actions.ts
file, we are defining an action usingcreateAction
, we are also using theprops
to specify that our action needs the fetched repos. - In the
app.component.ts
ngOnInit
we are fetching the repos and callingstore.dispatch
to set the repos in the store using the action we created. - In the
app.component.ts
we are using thestore.select
to select the repos from the store. - In the
reducer.ts
we are handling the action and setting the repos in the store.
createActionGroup
The createActionGroup
is used to create multiple actions at once, it’s useful when you want to create a group of actions that are related to each other (Usually we have groups of actions so you would probably use this function to create an action even more than createAction
).
The createActionGroup
function receives an object with 2 properties:
source
- The name of the group of actions (usually that name relates to the section of the state these actions relate to).events
- An object with the actions, the key is the name of the action and the value is the props for the action.
Student Exercise - createActionGroup
In this exercise you will need to create a group of actions using createActionGroup
.
- In the
actions.ts
file you will need to create a group of actions containing 2 actions:Set Repos
- Will be used to set the repos in the store (like the previous exercise).Delete Repo
- Will be used to delete a repo from the store (will get the repo id in the payload).
- In the
reducer.ts
file you will need to handle theDelete Repo
action and remove the repo from the store based on the id that you get from the action. - In the
app.component.ts
you will need to dispatch theDelete Repo
action when the user clicks the delete button.
Solution
Summary
Actions allow us to tell NGRX about something that happened in the app which might cause a state change.
We learned about the 2 api’s that NGRX supplies us for creating an action (createActionGroup
and createAction
).
For the majority of cases you will need to create a group of actions and will therefore use createActionGroup
.