What is the best way to retrieve the final value stored?

This is how I am using my Selector:-

private loadTree() {

    this.loading = true;

    this.store.select(transitionListSelector).pipe().subscribe(data => {
      console.log(data);
      data.map(item => {
        console.log(item);
        this.treeItems.push({ label: item.name, leaf: false, ...item });
      })
      this.loading = false;

    });

  }

This is the selector I have defined:-

export const transitionListSelector = createSelector(
    topicModuleSelector,
    (s) => s.transitionListState.transition
);

This is the reducer I am using:-

export const transitionListReducer = createReducer(
    initialTopicModuleState.transitionListState,
    on(actions.listTransitionAction, (s) => {
        return { ...s, transition: [] };
    }),
    on(actions.listTransitionSuccessAction, (s, { transition }) => {
        return { ...s, transition };
    })
);

export function transitionListReducerMap(s: any, a: Action): any {
    return transitionListReducer(s, a);
}

This represents my TopicModuleState structure:-

export interface TopicModuleState {
        
     transitionListState: TransitionListState;
    
} 
export interface TransitionListState {
    transition: Transition[];
}

export const initialTopicModuleState: TopicModuleState = {
    
    transitionListState: {
        transition: []
    }
};

here is an image for reference

In the console output, there are 2 results - first one empty and second one actual. I only need the actual result to be displayed.

Answer №1

Upon initialization of the store, selectors are assigned initial values. If you choose to omit this initial value, the skip operator can be utilized.

this.store.select(transitionListSelector).pipe(skip(1))

In addition, it is advisable not to assign an empty array when dispatching listTransitionAction. Instead, create a listTransitionActionFail action and utilize this action to set an empty array. Subsequently, migrate your loading state to the ngrx state.

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

Warnings about NgZone timeouts are displayed in Chrome DevTools even though the timeouts are actually executing outside of the

Is it common to receive a warning in Chrome DevTools like this? [Violation] 'setTimeout' handler took 103ms zone.js:1894 even when all timeouts are executed outside of ngzone? I personally follow this approach: this.zone.runOutsideAngular(() = ...

The issue with IONIC/Angular lies in its inability to properly render the JSON data retrieved

I have recently started learning IONIC/Angular and Javascript, although I do have some background in other programming languages. The issue I'm currently facing is related to fetching JSON data from an external API (I've created the API with Stra ...

employing ts as a reference for the pathway

Every time I reference path using "ts" I include the following code snippet: import * as fs from 'fs'; import * as path from 'path'; However, when I try to run node index.ts, an error occurs: import * as path from 'path'; ...

I'm facing some difficulties in sourcing my header into a component and encountering errors. Can anyone advise me on how to properly outsource my header?

Looking to streamline my headers in my Ionic 4 project by creating a separate reusable component for them. Here's how I set up my dashboard page with the header component: <ion-header> <app-header title="Dashboard"></app-he ...

Components on different routes are not being triggered by the `BehaviourSubject` subscription

In my current project, I am working on transferring data between components using a subject. I have created a service for this purpose, and the two components involved are imagescust and imagesipa. The data is being passed from the imagesipa component and ...

Generate a versatile Union type featuring a mapped property

I am currently working with different types of data enum DataTypes { Email = 'email', Checkbox = 'checkbox', } type DataTypeValues = { [DataTypes.Email]: string; [DataTypes.Checkbox]: boolean; }; type Type1<T extends DataTy ...

Playwright script encounters module not found error

I am currently facing an issue with implementing Playwright in my project. It seems that Playwright is struggling to a) resolve path aliases and b) it is unable to locate certain npm packages that have been installed. Here is the structure of my project: ...

Learn how to display data from the console onto an HTML page using Angular 2

I am working on a page with 2 tabs. One tab is for displaying active messages and the other one is for closed messages. If the data active value is true, the active messages section in HTML should be populated accordingly. If the data active is false, th ...

CombineReducers takes all the reducer content and unpacks it into a single reducer

As I work on developing a small application, I am contemplating the best strategy for organizing reducers within it. My objective is to retrieve JSON data from the application state and structure it as shown below: { "fruits": [ {"appl ...

Trigger Angular2 EventEmitter in child component to inform parent component

I am having trouble triggering an event from the child component to the parent component. @Component({ template:'<foo></foo>' }) export class ParentComponent{ onDoSomething($event){ //handling logic goes here } } @Compo ...

Loading an external javascript file dynamically within an Angular component

Currently, I'm in the process of developing an Angular application with Angular 4 and CLI. One of my challenges is integrating the SkyScanner search widget into a specific component. For reference, you can check out this Skyscanner Widget Example. T ...

Preventing special characters in an input field using Angular

I am trying to ensure that an input field is not left blank and does not include any special characters. My current validation method looks like this: if (value === '' || !value.trim()) { this.invalidNameFeedback = 'This field cannot ...

Ways to initiate SVG animations using Angular Component functions?

I am currently working on a project where I want to incorporate an animation that reflects the sorting process of an array of numbers. However, despite successfully sorting the numbers in the array, I am facing challenges with triggering the animations. I ...

Fetching DataItem from a Kendo Grid using a custom button in an Angular application

I have been working on transferring the dataItem from a Kendo grid to an Angular 6 component. Here is my setup: <kendo-grid [data]="view | async" [height]="533" (dataStateChange)="onStateChange($event)" (edit)="editHandler($even ...

Retrieving the Object value in Primeng p-dropdown when there is a change in selection

In my p-dropdown, I am trying to extract the selected value. <p-dropdown optionLabel="name" [options]="things" placeholder="Select Thing" [(ngModel)]="input" (onChange)="getValue(input)"></p-dropdown> typescript: //each lin ...

When using EmotionJS with TypeScript, the theme type is not properly passed to props when using styled components

CustomEmotions.d.ts import '@emotion/react'; declare module '@emotion/react' { export interface Theme { colors: { primaryColor: string; accentColor: string; }; } } MainApp.tsx import { ...

Set a value to the field name within a variable in TypeScript

Can anyone help me with this problem? type A { f1: string; f2; string; } I have a variable that holds the name of a field: let fieldName: string = "f2"; I want to create an object using the fieldName: {"content of fieldName": "sdf"} Any suggestio ...

Running the serve command necessitates being within an Angular project environment; however, despite this, Angular 4 was unable to locate a project definition

I recently cloned an old project from Github and ran into some vulnerabilities when trying to install node_module. In order to address these issues, I executed the following command: npm audit fix Despite running the above command, there were still unres ...

What is the best way to shorten text in Angular?

I am looking to display smaller text on my website. I have considered creating a custom pipe to truncate strings, but in my situation it's not applicable. Here's what I'm dealing with: <p [innerHTML]="aboutUs"></p> Due to t ...

The function cb() was never invoked, resulting in an error during the npm install process

I'm having trouble installing node modules in my angular project. Running npm install gives me this error: npm ERR! cb() never called! npm ERR! This is an error with npm itself. Please report this error at: npm ERR! <https://npm.community> ...