2

I work in a project with ngrx, also using other libraries & packages from ngrx like ngrx/entity or ngrx/data.

Sometimes I need to create effects in certains actions belonging (in terms of being dispatched) to ngrx/data.

Where I find some difficulties is dispatching other actions with strong dependencies of some other actions dispatched by ngrx/data in an automatic way or even by myself (custom) like below

...

    @Injectable()
    export class FooEffects {

        fooSelected$ = createEffect(() =>
            this.actions$
                .pipe(
                    ofType(FooActions.fooSelected),
                    map((action) => this.entityActionFactory.create<Foo>('Foo', EntityOp.QUERY_LOAD),
                  ))
);
       ...

        constructor(private actions$: Actions, private entityActionFactory: EntityActionFactory) { }

    }

...

So, how do I listen Actions in ngrx/data like QUERY_LOAD, the common get/all or any other else to achieve the creation of new effects? Does it exist something like

fooNgrxDataEffectForQueryLoad$ = createEffect(() =>
            this.actions$
                .pipe(
                    ofType(('Foo', EntityOp.QUERY_LOAD)),

Or asked in another way, how do I write effects for "NATIVE" actions in ngrx/data?

I know ngrx/data was created to get rid off from of Actions, Selectors, Effects, Reducers as much as it is possible, having therefore less boilerplate. However, there should be a way to create effects from ngrx/data.

Many thanks in advance.

2

2 Answers 2

1

You can use ofEntityType and ofEntityOp pipeable operators to write effects for NgRx Data actions. So, in your example above, you could say:

 fooNgrxDataEffectForQueryLoad$ = createEffect(() =>
            this.actions$
                .pipe(
                    ofEntityType('Foo'),
                    ofEntityOp(EntityOp.QUERY_LOAD)),

Looks like the OP answered his own question in this article as well: https://betterprogramming.pub/write-effects-for-actions-in-ngrx-data-214c93869758

1
  • Exactly, I did heh...Hope it helps :)
    – ackuser
    Jun 1, 2023 at 10:13
0

What you have in your example is one of possible ways.

You should listen on actions of ngrx/data type and cause stuff you need. ngrx/data is quite raw and has a lot of limitations.

What they recommend is to extend base classes and to change behavior there adding new actions / effects / reducers etc.

Would be nice if you could provide an example of how you want to change the flow.

3
  • I appreciate your answer :), but sorry if I explained myself not very good...The main question was "how do I do effects for actions in ngrx/data". Thx
    – ackuser
    May 7, 2020 at 6:15
  • Ah. The same as you wrote above. The goal of Nrgx/data is to get rid of mess of actions effects and reducers via provided services. If you want to extend one - you need extend a base class and override its methods. Or you can do like you have in your question - listen on these effects types and do own stuff. Might you give an example of what you would like to do in an effect with ngrx data?
    – satanTime
    May 7, 2020 at 7:14
  • Updated my answer with the same things as in the comment.
    – satanTime
    May 7, 2020 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.