You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: projects/ngrx.io/content/guide/store/action-groups.md
+64-35Lines changed: 64 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,20 @@
1
1
# Action Groups
2
2
3
-
Action groups is a feature to group actions together that belong to the same source. While writing actions, the actions in most of the cases looks like below.
3
+
<divclass="video-container">
4
+
<divclass="video-responsive-wrapper">
4
5
5
-
<code-exampleheader="products-page.actions.ts">
6
-
import { createAction, props } from '@ngrx/store';
7
-
8
-
export const opened = createAction('[Products Page] Opened');
In the example we can see that the source (`[Products Page])` is duplicated within each action. With the help of the `createActionGroup` API this can be avoided.
23
-
The next example leverages `createActionGroup` to group actions together that belong to the same source. This makes that defining actions is more compact.
16
+
The `createActionGroup` function creates a group of action creators with the same source.
17
+
It accepts an action group source and an event dictionary as input arguments, where an event is a key-value pair of an event name and event props.
24
18
25
19
<code-exampleheader="products-page.actions.ts">
26
20
import { createActionGroup, emptyProps, props } from '@ngrx/store';
// defining an event without payload using the `emptyProps` function
32
26
'Opened': emptyProps(),
33
-
27
+
34
28
// defining an event with payload using the `props` function
35
29
'Pagination Changed': props<{ page: number; offset: number }>(),
36
-
30
+
37
31
// defining an event with payload using the props factory
38
32
'Query Changed': (query: string) => ({ query }),
39
-
}
33
+
},
40
34
});
41
35
</code-example>
42
36
43
-
To dispatch an action from the group, import the group and invoke an action.
44
-
This returns an action that you can then dispatch to the store.
37
+
<divclass="alert is-helpful">
38
+
39
+
The `emptyProps` function is used to define an action creator without payload within an action group.
40
+
41
+
</div>
45
42
46
-
```typescript
43
+
If we create a new action creator using the `createAction` function by copying the previous one but accidentally forget to change its type, the compilation will pass.
44
+
Fortunately, this is not the case with the `createActionGroup` function because we will get a compilation error if two actions from the same group have the same type.
45
+
46
+
The `createActionGroup` function returns a dictionary of action creators where the name of each action creator is created by camel-casing the event name, and the action type is created using the "[Source] Event Name" pattern.
47
+
Also, there is no longer a need for barrel files or named imports because the action group can be imported directly into another file.
48
+
49
+
<code-exampleheader="products.component.ts">
50
+
import { Component, inject, OnInit } from '@angular/core';
51
+
import { Store } from '@ngrx/store';
47
52
48
53
import { ProductsPageActions } from './products-page.actions';
49
54
50
55
@Component({ /* ... */ })
51
56
export class ProductsComponent implements OnInit {
In the previous example, event names are defined in the title case format.
81
+
In that case, it can be challenging to search for unused action creators because their names are automatically generated by camel-casing the event names.
69
82
70
-
```
83
+
The `createActionGroup` function provides the ability to define event names in the camel case format as well, so action creators will have the same names as events.
84
+
This makes it easier to search for their usage within the codebase.
85
+
86
+
<code-exampleheader="products-api.actions.ts">
87
+
import { createActionGroup, props } from '@ngrx/store';
0 commit comments