NGRX Actions
This video explains how to use NGRX actions in Angular, including creating and dispatching actions, with practical examples and student exercises.
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?
Section titled “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
Section titled “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.
import { createAction, props } from '@ngrx/store';
export const setRepos = createAction( '[GroupName] Identifier For Action', props<{ additionalData: any, anotherData: any, asManyDataAsYouWant: any, anyNameHereYouWant: any }>());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
Section titled “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.tswhich will display a list of repositories from github. - The app contains
actions.tswhere you will need to create a single action - The app contains
reducer.tswhich responds to the action you will create. - The file
repos.service.tscontains a service that will fetch the repositories from github. - The files you will modify in the exercise are:
app.component.ts,actions.tsandreducer.ts(maybe), the rest of the files will be read only files. - You goal is that the
app.component.tswill display the list of repositories from github, but grab that list from the store. - The
app.component.tswillimplement OnInitand in thengOnInitmethod you will need to use therepos.service.tsto grab the repos and then populate the ngrx state with those repos. - You will need to create an action in
actions.tsthat will be dispatched from theapp.component.tsand will be handled by the reducer inreducer.ts.
Solution
Section titled “Solution”Here is the solution for the exercise:
- In the
actions.tsfile, we are defining an action usingcreateAction, we are also using thepropsto specify that our action needs the fetched repos. - In the
app.component.tsngOnInitwe are fetching the repos and callingstore.dispatchto set the repos in the store using the action we created. - In the
app.component.tswe are using thestore.selectto select the repos from the store. - In the
reducer.tswe are handling the action and setting the repos in the store.
createActionGroup
Section titled “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).
import { createActionGroup, props } from '@ngrx/store';import type { Repo } from './repo';
export const repoActions = createActionGroup({ source: 'Repos', events: { 'Set Repos': props<{ repos: Repo[] }>(), },})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
Section titled “Student Exercise - createActionGroup”In this exercise you will need to create a group of actions using createActionGroup.
- In the
actions.tsfile 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.tsfile you will need to handle theDelete Repoaction and remove the repo from the store based on the id that you get from the action. - In the
app.component.tsyou will need to dispatch theDelete Repoaction when the user clicks the delete button.
Solution
Section titled “Solution”Summary
Section titled “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.