Using a combination of multiple pipeable selectors from NgRx

Can we combine multiple pipeable selectors in the same manner as regular selectors?

Combining regular selectors:

export const selectActiveOrganization = createSelector(
    selectOrganizations,
    selectActiveOrganizationId,
    (organizations, activeOrgId) => organizations[activeOrgId],
);

In the example above, two selectors selectOrganizations and selectActiveOrganizationId are used to create selectActiveOrganization using createSelector.

The issue here is that selectActiveOrganization can potentially return undefined.


Let's now convert this into a pipeable selector:

const _selectActiveOrganization = createSelector(
    selectOrganizations,
    selectActiveOrganizationId,
    (organizations, activeOrgId) => organizations[activeOrgId],
);

export const selectActiveOrganizationPipeable = pipe(
    select(_selectActiveOrganization),
    filter(activeOrg => !!activeOrg),
);

In selectActiveOrganizationPipeable, we have enhanced the original selector to only emit a value if there is an active organization.

Now let's assume we want to extend our pipeable selector to extract only the organization name.

export const selectActiveOrganizationNamePipeable = pipe(
    select(selectActiveOrganizationPipeable),
    map(organization => organization.name), // --> TS ERROR: Property 'name' does not exist on type 'Observable<Organization>'.ts(2339)
);

Is it possible to reuse the existing selectActiveOrganizationPipeable? (similar to how we reuse regular selectors)

Answer №1

This method is proven to be effective. Utilize the existing pipe functionality within a new pipe.

const customOrganizationNameSelector = 
  pipe(
    selectActiveOrganizationPipeable,
    map(organization => organization.name)
  );

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

How can one accurately identify if a URL belongs to an image file?

How can one effectively determine if a URL belongs to an image file? ...

The specified element at coordinates (x,y) could not be found: solutions for clicking on a series of WebElements individually

Currently, I am tasked with a project that involves taking user input, scraping data from TripAdvisor.it based on that input, storing it in a database, and using machine learning to rearrange the data for future requests. This process aims to offer users c ...

Issue with Angular Material Slide Toggle Binding on Checked Status Not Functioning

It should be a simple task, but I'm struggling to make the checked binding for the slide toggle function properly. (My setup includes angular version 9.1 and material version 9.2.) Here is the HTML snippet: <mat-slide-toggle [checked]="isSlideCh ...

JQuery could not retrieve the property 'json' from an undefined or null reference

Currently, I am in the process of developing a Windows 8 app using HTML and accessing data from an API. Unfortunately, I keep encountering an error: 0x800a138f - JavaScript runtime error: Unable to get property 'json' of undefined or null refer ...

What could be causing the issue of PHP not receiving this multidimensional array through Ajax?

Having an issue with receiving a multidimensional array in PHP after posting it from JS using Ajax: $.ajax({ type: 'post', url: 'external_submit.php', dataType: "json", data: { edit_rfid_changes_submit ...

Execute JavaScript function after completion of CSS animation using jQuery

I'm working on an accordion feature that uses CSS animation to expand the clicked item. The expansion is triggered by li:target. However, I'm facing an issue where when clicking on an item, the scroll position doesn't align correctly with t ...

What is the best way to create a reusable component for a Material-UI Snackbar?

Having trouble getting my Alert component to display a message that says "Successfully submitted" in the parent component. The message doesn't seem to be showing up. AlertComponent import React, { useState } from "react"; import { Snackbar, Alert } f ...

Tips on transforming data stored in an array into a nested array structure using Angular 13

Just starting out with Angular and I'm looking to convert my data into a nested array format in Angular 13. If anyone has a StackBlitz example, that would be greatly appreciated! This is the data I've retrieved from a random API: "data& ...

Showing the contents of an array which contains arrays

Retrieves the elements stored in the history array. Each element of the history array is a separate array containing fields with names and values. The goal is to display this history table by iterating over each element of the array and displaying it in ...

Issue with Angular forms: The value of the first input element does not match the value set

I am still learning Angular, so please forgive me if my question seems a bit basic. Currently, I have a reactive form that retrieves data for editing from my controller. It seems to be working but there are some bugs present. Controller: myForm:any ...

Implementing a JavaScript validator for an ASP textbox

I have come across many posts discussing how to prevent an ASP textbox from accepting only numbers. However, I am looking for a JavaScript example that can check if the entered value falls between 0 and 100. My validator currently checks if each key entere ...

The Highchart chart is currently experiencing compatibility issues on both Chrome and Firefox

After designing a Highchart for my report, I encountered an issue where it wasn't displaying on Chrome and Firefox browsers. However, the chart was visible when using IE after enabling ActiveX. Can someone provide insight into why this discrepancy occ ...

If the first returned function of the compose function is called without any arguments, what is the starting value of its reduce function?

In my attempts to modify the compose function to output to the console using different arguments, I am struggling to find a solution. The initial call of the compose function results in an undefined argument arg. Consequently, the reduce function utilizes ...

Display the full image size when the mouse is hovered over

I currently have two links for a single image. One link is a thumbnail displayed on the screen, while the other link leads to the full-size version of the same image. Is there a way I can implement a code that will darken the screen and display only the f ...

Tips for updating JS and CSS links for static site deployment

As I develop my static site using HTML files served from a simple web server, I want to use local, non-minified versions of Javascript code and CSS files. However, when it comes time to deploy the site, I would like to refer to minified versions of these s ...

Navigate through a given value in the event that the property undergoes a name change with each

Exploring an example found on the JSON site, I am facing a situation where I am using an API with JSON data. The property name "glossary" changes for each request made. For instance, if you search for "glossary", the first property is glossary. However, if ...

Display Page Separation with JavaScript

I am working on a project where I have created payslips using Bootstrap through PHP. To maintain organization, I am looking to create a new page after every 6 payslips. Below is the code snippet that I am using to generate the payslips: foreach($results a ...

Display validation errors in Angular2 forms when the form items are left empty and the user tries to submit the form

In my application, I have a userForm group containing keys such as name, email, and phone. Additionally, there is an onValueChanged function that subscribes to changes in the form and validates the data. buildForm(): void { this.userForm = this.fb.gr ...

Using a for loop in Angular's OnInit method to populate an array results in an array that

Firstly, within the ngOnInit function, I populate the items array: id: string = ''; items: Item[] = []; ngOnInit(): void { this.id = this.activatedRoute.snapshot.params['id']; this.itemService.getItems(this.id).subscribe({ ...

Problems with spacing in Slick slider and lazyYT integration

Utilizing lazyYT helps to enhance the loading speed of YouTube videos. Once loaded, these lazyYT videos are then placed within a slick slider. However, an issue arises where the videos stick together without any margin between them. To address this problem ...