Extracting data from an action using NgRx8

Hey everyone, I'm looking for some guidance on how to destructure action type and props in an ngrx effect. I'm struggling with this and could use some help!

This is my list of actions:

export const addTab = createAction(
  '[SuperUserTabs] add tab',
  props<{ tab: SuperUserHeaderTab, tabType: TabType }>()
);

export const searchCompanyTab = createAction(
  '[SuperUserTabs] search company tab'
);

export const searchCardholderTab = createAction(
  '[SuperUserTabs] search cardholder tab'
);

Here's the effect code:

@Effect({ dispatch: false })
  addTab$ = this.actions$.pipe(
    ofType(
      TabsActions.addTab,
      TabsActions.searchCompanyTab,
      TabsActions.searchCardholderTab
    ),
    withLatestFrom(this.store.pipe(select(getTabs))),
    tap(([action, tabs]) => {
      // My issue lies here 
      const {type, props} = action;
      // additional logic
    })
  );

Any advice or suggestions would be greatly appreciated!

Answer №1

Revamped: ofType allows you to directly use the accept action creator in this manner:

@Effect({ dispatch: false })
  addTab$ = this.actions$.pipe(
    ofType(
      TabsActions.addTab,
      TabsActions.searchCompanyTab,
      TabsActions.searchCardholderTab
    ),
    withLatestFrom(this.store.pipe(select(getTabs))),
    tap(([action, tabs]) => {
      // this object will include `type` and action payload
      const {type, ...payload} = action; 
      // some logic
      console.log(payload)
    })
  );

============== The previous answer remains valid:

Since you are utilizing an action creator that returns a function, you can access the action type through the type property like so:

@Effect({ dispatch: false })
  addTab$ = this.actions$.pipe(
    ofType(
      TabsActions.addTab.type,
      TabsActions.searchCompanyTab.type,
      TabsActions.searchCardholderTab.type
    ),
    withLatestFrom(this.store.pipe(select(getTabs))),
    tap(([action, tabs]) => {
      // this object will contain `type` and action payload
      const {type, ...payload} = action; 
      // some logic
      console.log(payload)
    })
  );

demo: https://stackblitz.com/edit/angular-3t2fmx?file=src%2Fapp%2Fstore.ts

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Employing a provider within a different provider and reciprocally intertwining their functions

I'm currently facing an issue with two providers, which I have injected through the constructor. Here's the code for my user-data.ts file: @Injectable() export class UserDataProvider { constructor(private apiService: ApiServiceProvider) { ...

Encountering a discord bot malfunction while on my Ubuntu server

My discord bot runs smoothly on my Windows 10 setup, but when deployed to the server running Ubuntu Server 20, it encounters a specific error. The issue arises when trying to handle incoming chat messages from the server. While I can read and respond to m ...

Positioning Data Labels Outside of Pie or Doughnut Charts in Chart.js

I am currently working on a large-scale project and I am in need of a way to position the labels outside of pie charts or doughnut charts. I came across a plugin called outerLabels on GitHub which seems to be the perfect solution for what I need, but I am ...

Disappearing act: Ionic tabs mysteriously disappear when the back button

Whenever I navigate in my ionic app, I notice that the tabs-bar disappears when I go to different pages and then return to the tabs. See Demo Code tab1 Here is a sample link to navigate to other pages: <ion-label routerDirection="forward" [routerLi ...

Error: Module 'react' not found. Please make sure it is installed and correctly imported

Recently, I've been working on developing a React app using TypeScript. To kickstart the project, I used yarn create react-app (name) --use-pnp --typescript. However, I encountered an issue with the TS linter repeatedly showing the error: Cannot find ...

Leveraging the Power of SASS Variables Across Your Entire NativeScript Application

Currently, I am in the process of developing an app using NativeScript 6.4.1 along with Angular 8. In order to enhance my styling capabilities, I have decided to incorporate SASS due to its advantageous features like variable names. My designer has suppli ...

Typescript polymorphism allows for the ability to create various

Take a look at the following code snippet: class Salutation { message: string; constructor(text: string) { this.message = text; } greet() { return "Bonjour, " + this.message; } } class Greetings extends Salutation { ...

What is the best way to create a custom type guard for a type discriminator in TypeScript?

Suppose there are objects with a property called _type_ used to store runtime type information. interface Foo { _type_: '<foo>'; thing1: string; } interface Bar { _type_: '<bar>' thing2: number; } function helpme(i ...

Using ngFor in Angular 6 to create a table with rowspan functionality

Check out this functional code snippet Desire for the table layout: <table class="table table-bordered "> <thead> <tr id="first"> <th id="table-header"> <font color="white">No.</font> </th> <th id="table-hea ...

Mastering the art of bi-directional data binding with nested arrays in Angular

Imagine you have a to-do list with various tasks, each containing multiple subtasks. You want the ability to change the subtask data, but why is Angular not properly two-way binding the data for the subtasks? HTML <div *ngFor="let task of tasks"> ...

Ensure that Angular CLI libraries contain all necessary dependencies

I'm currently facing a challenge with integrating Angular Material dependency into my custom library. The main application that references the custom library encounters an error stating "Cannot find @angular/material/core" and "@angular/material/tabs" ...

The infer keyword fails to provide the intended result due to the absence of a required property

Summary: interface CoreProps {prop1: number;} interface AnchorProps {prop2: number} type Feature<P extends object> = { state?: (state: object, props: P) => object | void; }; type FeaturePT<P extends object> = { state?: (state: object, props ...

Is there a way for me to determine the value that has been assigned to a <li> key attribute in React using Jest and testing-library/react?

In my current project, I am using a combination of React with TypeScript and Jest along with Testing Library for testing purposes. I have a specific requirement to unit test some code where I need to ensure that the person.id is correctly set as the key at ...

Is there a way for me to extract information from a static HTML page, like the meta tag, in a simple manner

In my Angular 2 application, I am using Flask (Python framework) to serve up the static HTML content when accessed through the index. My goal is to show the version of the application on the AboutComponent. Currently, I have Flask injecting the version int ...

Utilizing Ionic to import and parse an Excel file for data processing

I need assistance in uploading an Excel file and reading it using Ionic-Angular. I tried the following code, but it only seems to work for browsers and not for the Ionic app on Android. Can someone please help me with this issue? I am trying to read the E ...

Could we apply 'ngZone: 'noop' specifically to a function or component in Angular?

I am currently utilizing a third-party library to create PowerPoint presentations in Angular (Version 5). The third-party library makes numerous asynchronous calls and promises, causing zone.js to keep track of more than 50 loops running simultaneously. Th ...

Attempting to successfully upload this Angular 7 form to my TypeScript code. Making use of ngForm and [(ngModel)] to achieve this

I am having trouble passing form information using the onSubmit() function. It seems to be undefined when I try to execute it initially. Could there be a syntax error that I'm missing? <form class="gf-formbox" name="credentials" (ngSubmit)="onSubm ...

Jhipster entity authentication

For my latest project with JHipster, I decided to incorporate Angular 2 and MongoDB. One of the entities I created is a 'doctor' with attributes such as name (string), login (string), password (string), and specialty (string). Now I want to enabl ...

Importing 100 .ts files in a dynamic manner

Forgive me for my lack of experience in TypeScript, but I have a query regarding loading multiple .ts files. Imagine I have a directory containing 100 .ts files. Is it appropriate to load all these files using the fs module, as shown below? readdirSync(__ ...

Tips for activating AG Grid Column Auto Sizing on your website

The Issue I am experiencing difficulty in getting columns to expand to the size of their content upon grid rendering. Despite following the guidance provided in the official documentation example, and consulting sources such as Stack Overflow, I have att ...