qkhanhpro
qkhanhpro

Reputation: 5230

NgRx: Correct way to dispatch an action with data dependency

Currently, in my project using NgRx, I have a facade that contains some functions:

LoadMyData()
{
    dispatch(MyActions.LoadMyDataAction({ SomeDependentData }))
}

I also have

  myDependentData$ = this.store.pipe(
    select(MySelectors.GetMyData)
  );

What is the right way to get and pass value to SomeDependentData from myDependentData$? Or there should be some improvement in this design?

One of my colleagues says that I should be implementing an effect that catches my action and resolve the selector then feed the resolved value to the same action, Is this approach possible / good / bad?

Upvotes: 0

Views: 489

Answers (1)

timdeschryver
timdeschryver

Reputation: 15505

Depends, using a selector in an effect works. Another option is to unwrap the selector's value in the template (using the async pipe) and pass it along to the load method:

LoadMyData(data: any)
{
    dispatch(MyActions.LoadMyDataAction({ SomeDependentData, extra: data }))
}

Upvotes: 1

Related Questions