@@ -24,15 +24,28 @@ import { initEffects } from '@ngneat/effects';
24
24
initEffects ();
25
25
```
26
26
27
- Next, we need to define our actions. For example :
27
+ Actions are created by using the ` createAction ` or ` actionsFactory ` functions :
28
28
29
29
``` ts
30
- import { createAction } from ' @ngneat/effects' ;
30
+ import { actionsFactory , createAction } from ' @ngneat/effects' ;
31
+
32
+ // todos.actions.ts
33
+ export interface Todo {
34
+ id: string ;
35
+ name: string ;
36
+ }
31
37
32
38
export const addTodo = createAction (' [Todos] Add Todo' , props < { title: string });
39
+
40
+ // Ee recommend using the actions factory to prefix each action for better readability and debug purposes when using redux dev tools
41
+ export const todoActions = actionsFactory (' todo' );
42
+
43
+ // We can declare an action by passing it a type and an optional payload.
44
+ export const loadTodos = todoActions .create (' Load Todos' )
45
+ export const addTodo = todoActions .create (' Add Todo' , props <Todo >())
33
46
```
34
47
35
- Next, we need to define the effects, and register them:
48
+ Next, we need to define the ` effects ` , and register them:
36
49
37
50
``` ts
38
51
import { createEffect , registerEffects , ofType } from ' @ngneat/effects' ;
@@ -72,13 +85,10 @@ export function TodosPage() {
72
85
useEffect (() => dispatch (loadTodos ()), []);
73
86
74
87
return (
75
- < button onClick = {()
76
- =>
77
- dispatch(addTodo ({ title : ' foo' }))
78
- }>
79
- Add
80
- < / button >
81
- )
88
+ < button onClick = {() => dispatch(addTodo ({ title : ' foo' }))}>
89
+ Add
90
+ < / button >
91
+ )
82
92
}
83
93
```
84
94
@@ -88,90 +98,61 @@ The effects we pass are tied to the component life cycle hook and will be destro
88
98
89
99
First, install the package: ` npm i @ngneat/effects-ng ` .
90
100
91
- Then we need to register our root effects in our app module.
101
+ Next, create the ` effect ` provider:
92
102
93
103
``` ts
94
- import { TodoEffects } from " todo/todo.effect.ts "
104
+ import { createEffect } from ' @ngneat/effects ' ;
95
105
96
- // app.module.ts
97
- EffectsNgModule . forRoot ([ TodoEffects ])
106
+ @ Injectable ({ providedIn: ' root ' })
107
+ export class TodosEffects {
98
108
99
- @NgModule ({
100
- imports: [
101
- EffectsNgModule .forRoot ([TodoEffects ]),
102
- ]
103
- })
104
- export class AppModule {
109
+ constructor (private todosApi : TodosApi ) {}
110
+
111
+ loadTodos$ = createEffect (actions =>
112
+ actions .pipe (
113
+ ofType (loadTodos ),
114
+ switchMap ((todo ) => this .todosApi .loadTodos ())
115
+ )
116
+ );
105
117
}
106
118
```
107
119
108
- In order to register lazily loaded effects use the forFeature method.
120
+ Then we need to register our the ` effects ` in our app module:
109
121
110
122
``` ts
111
- import { UserEffects } from " user/user.effect.ts"
123
+ import { EffectsNgModule } from ' @ngneat/effects-ng' ;
124
+ import { TodosEffects } from ' todos/todos.effect.ts' ;
112
125
113
- // lazy.module.ts
114
126
@NgModule ({
115
127
imports: [
116
- EffectsNgModule .forFeature ([ UserEffects ])
128
+ EffectsNgModule .forRoot ([ TodosEffects ]),
117
129
]
118
130
})
119
- export class LazyModule {
131
+ export class AppModule {
120
132
}
121
133
```
122
134
123
- An injectable effects class holds properties and uses dependency injection as you're familiar from other Angular
124
- injectable services.
135
+ In order to register lazily loaded effects use the ` forFeature ` method:
125
136
126
137
``` ts
127
- // todo.effect.ts
128
- @Injectable ()
129
- export class TodoEffects {
130
-
131
- constructor (
132
- private todoApiService : TodoApiService
133
- ) {
134
- }
138
+ import { EffectsNgModule } from ' @ngneat/effects-ng' ;
139
+ import { PostsEffects } from " posts/posts.effect.ts"
135
140
136
- loadTodos = createEffect (actions => actions .pipe (
137
- ofType (loadTodos ),
138
- switchMap ((todo ) => this .todoApiService .loadTodos ()),
139
- map (({ todos }) => actions .dispatch (showSnackbar ({ message: ` Todos loaded. ` })))
140
- ));
141
- }
142
- ```
143
-
144
- Effects listen to actions that can be triggered by using the actions stream and its dispatch method
145
-
146
- ``` ts
147
- // todo.action.ts
148
- export interface Todo {
149
- id: string ;
150
- name: string ;
151
- status: " not started" | " in progress" | " done"
141
+ @NgModule ({
142
+ imports: [
143
+ EffectsNgModule .forFeature ([PostsEffects ])
144
+ ]
145
+ })
146
+ export class LazyModule {
152
147
}
153
-
154
- // we recommend using the actions factory to prefix each action for better readability and debug purposes when using redux dev tools
155
- const todoActions = actionsFactory (" todo" )
156
-
157
- // we can declare an action by passing it a type and an optional payload.
158
- export const loadTodos = todoActions .create (" Load Todos" )
159
- export const addTodo = todoActions .create (" Add Todo" , props <Todo >())
160
-
161
148
```
162
149
163
- If we don't need the reactive behavior of effects and we want to call an effect without using actions we can use the
164
- effect functions as explained [ below] ( #angular-effect-functions ) .
165
-
166
- The actions can be dispatched wherever we have access to the actions stream. Common use cases are dispatching actions in
167
- components or within other effects
150
+ The actions can be dispatched by injecting the ` Actions ` provider:
168
151
169
152
``` ts
170
- // app.component.ts
171
153
@Component (... )
172
154
export class AppComponent {
173
- constructor (private actions : Actions ) {
174
- }
155
+ constructor (private actions : Actions ) {}
175
156
176
157
ngOnInit() {
177
158
this .actions .dispatch (loadTodos ());
@@ -204,16 +185,12 @@ First, install the package: `npm i @ngneat/effects-hook`.
204
185
We can register the effect in our component, and call it when we need:
205
186
206
187
``` ts
207
- import { useComponentEffects$ } from ' @ngneat/effects-hooks' ;
188
+ import { useEffectFn } from ' @ngneat/effects-hooks' ;
208
189
209
190
function SearchComponent() {
210
- const searchTodo = useComponentEffects (searchTodoEffect );
191
+ const searchTodo = useEffectFn (searchTodoEffect );
211
192
212
- return <input onChange = {({ target : { value } })
213
- =>
214
- searchTodo (value )
215
- }
216
- />
193
+ return <input onChange = {({ target : { value } }) => searchTodo (value ) }/>
217
194
}
218
195
```
219
196
@@ -223,25 +200,24 @@ We can also register multiple effects:
223
200
224
201
```ts
225
202
function FooComponent() {
226
- const [addTodo, updateTodo, deleteTodo] = useComponentEffects ([
203
+ const [addTodo , updateTodo , deleteTodo ] = useEffectFn ([
227
204
addTodoEffect , updateTodoEffect , deleteTodoEffect
228
205
]);
229
206
230
- return
231
- ...
207
+ return ...
232
208
}
233
209
```
234
210
235
211
<h3 id="angular-effect-functions">Use with Angular</h3>
236
212
237
213
First, install the package: ` npm i @ngneat/effects-ng ` .
238
214
239
- Create an effect class, extends the ` ComponentEffects ` and use the ` createEffectFn ` to create your effects:
215
+ Create an effect class , extends the ` EffectFn ` and use the ` createEffectFn ` to create your effects:
240
216
241
217
` ` ` ts
242
- import { ComponentEffects } from ' @ngneat/effects-ng' ;
218
+ import { EffectFn } from '@ngneat/effects-ng';
243
219
244
- export class TodosEffects extends ComponentEffects {
220
+ export class TodosEffects extends EffectFn {
245
221
246
222
searchTodo = this.createEffectFn((searchTerm$: Observable<string>) => {
247
223
return searchTerm$.pipe(
0 commit comments