Defining the NgRx root state key within the application state interface

Here is an example of a selector taken from the NgRx documentation:

import { createSelector } from '@ngrx/store';

export interface FeatureState {
    counter: number;
}

export interface AppState {
    feature: FeatureState;
}

export const selectFeature = (state: AppState) => state.feature;

export const selectFeatureCount = createSelector(
    selectFeature,
    (state: FeatureState) => state.counter
);

I am facing an issue where this code does not work for me unless I also include the key of the root state defined in app.module.ts. For example, if I have the following setup in app.module.ts:

StoreModule.forRoot({rootKey: reducer}),

In order to make my selector work, I have to modify it like this:

export const selectFeature = (state: AppState) => state.rootKey.feature;

However, doing this results in an error because it deviates from my original AppState interface.

What could be causing this issue? What am I doing wrong?

Answer №1

This is my configuration process

export interface State {
    router: fromRouter.RouterReducerState<any>;
}

export const ROOT_REDUCERS_TOKEN = new InjectionToken<
    ActionReducerMap<State, Action>
>("Root reducers token", {
    factory: () => ({
        router: fromRouter.routerReducer
    })
});

export function actionLogger(reducer: ActionReducer<State>): ActionReducer<State> {
    return (state: State, action: any): any => {
        const result = reducer(state, action);
        console.groupCollapsed(action.type);
        console.log("previous state", state);
        console.log("action taken", action);
        console.log("resulting state", result);
        console.groupEnd();

        return result;
    };
}

export const additionalReducers: Array<MetaReducer<State>> = !environment.production
    ? [actionLogger]
    : [];

Inside my app.module.ts file

StoreModule.forRoot(ROOT_REDUCERS_TOKEN, {
  metaReducers: additionalReducers,
  runtimeChecks: {
    strictStateImmutability: true,
    strictActionImmutability: true,
    strictStateSerializability: true,
    strictActionSerializability: true,
  },
}),

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

Utilizing Angular Material Table to present information efficiently

I have a unique data structure that I need to present using mat-table. dataSource= [[1,2],[3,4],[5,6]] In this structure, dataSource[0] always serves as the heading, with the rest of the array items representing its rows. Therefore, the expected output ...

Setting up Typescript error handling for next-auth getProviders configuration

I recently started learning Typescript and came across a tutorial using next-auth. However, I encountered an error while following the tutorial with Typescript when using the getProviders function. https://i.stack.imgur.com/H5LaL.png https://i.stack.imgu ...

How can we transfer or exclude all boolean properties from one class to another or a "type"?

Within my Nestjs application, there is an entity class structured like this: @ObjectType() export class Companies { @Field(() => Int) @PrimaryGeneratedColumn({ type: 'int', name: 'id' }) public id: number; @Field() @Column ...

When utilizing create-next-app, an error may occur stating that the produced JSX.Element cannot be assigned to a variable

After creating a new project with TypeScript using create-next-app, I encountered an error in the default homepage when opened in my IDE (WebStorm). The error message reads: "Initializer type () => JSX.Element is not assignable to variable type NextPa ...

Discover the most effective method for identifying duplicate items within an array

I'm currently working with angular4 and facing a challenge of displaying a list containing only unique values. Whenever I access an API, it returns an array from which I have to filter out repeated data. The API will be accessed periodically, and the ...

What is the best way to add elements using JavaScript when a particular character is present?

Current: <p id="article">忙著端出 高階 DSLR 產品的 Nikon ,總算想到了在 兩年半之後 更新自己的入門系列數位單眼相機,端出 Nikon D3400。很好的產品</p> I want to divide the text whenever certain charac ...

Tips for displaying a single item in Angular2 and Ionic2

I am new to Angular and seeking help as I am facing an issue with my NodeJS server running socket.io. Despite trying to find a solution online, I have been unsuccessful so far. Here is my server code: SERVER var socket = require('socket.io'), ...

Heroku NodeJS - Page Not Found: Error 404

I recently set up a Heroku server with BootBot running on it, but I'm facing challenges while trying to render an HTML page from it. Here is what I have in my code: var app = express(); var path = require('path'); app.use(express.static(pat ...

To successfully handle this file type in Next.js, make sure you have the necessary loader configured as no loaders are currently set up to process this specific file

I encountered an issue when trying to load an image from a local directory in my Next.js application Failed to compile ./pages/components/image.png 1:0 Module parse failed: Unexpected character '' (1:0) You may need an appropriate loader to hand ...

Mastering the art of utilizing $.get(url, function(data) in pure JavaScript

I am currently exploring how to achieve the equivalent of $.get(url,function(data) { code }; in pure JavaScript. function fetchImage(keyword){ if(!ACCESS_KEY){ alert("Please update your access key"); return; } var request = new XMLHttpRequ ...

Sending a JavaScript array along with a form using the jQuery ajax function

I am working on an ASP.NET MVC5 app where I have a JavaScript array called 'selectElementList' that contains multiple data. I am attempting to post back this data along with form values using an AJAX jQuery function. While I am able to get the fo ...

Repairing the orientation in unique threejs capsule geometric shape

Exploring the realm of custom geometry in three.js, I decided to experiment with modifying Paul Bourke's capsule geometry example. However, as I delve into creating my own custom capsule geometry, I have encountered two main challenges: The orienta ...

Yes, it's not able to retrieve the value from headlessui combobox

I have encountered an issue while using the Headlessui combobox component in conjunction with Yup. Despite successfully storing the selected value in the selectedMemory state variable, Yup consistently generates a required error message. I seem to be overl ...

What is the best way to extract the elements within a form using Angular and automatically add them to a list?

Recently, I started learning Angular and decided to create a simple list feature. The idea is to input an item name and price, then click "Add item" to see it added to the list below. I have all the code set up correctly, but for some reason the name and ...

Using withRouter() to redirect will display all components

I'm brand new to Reactjs and am currently diving into routing. In my journey, I stumbled upon the use of withRouter() for programmatic redirection. I had assumed the flow would follow: constructor->Willmount->render, but it seems the current or ...

Issue with sending Angular 7 HttpClient post request to Node.js server

Running my Angular 7 app with ng serve on port 4200, I have a node server inside a docker container at localhost:8081. Postman confirms the server is up, with a successful POST request to localhost:8081/login. When clicking the login button in the login. ...

Ensuring draggable div remains fixed while scrolling through the page

I am currently working on creating a draggable menu for my website. The menu is functional and can be moved around the page as intended. However, I have encountered an issue where the menu disappears when scrolling down the page due to its position attrib ...

How can you create a MUI textfield that only allows numerical values to be entered in the password field?

Need help setting up input type password to only accept numeric values. Looking to toggle the eye icon to switch between hidden password type and visible numeric type. Currently, when hidden, it changes to text type, but I want to keep i ...

How can we determine if the mat-datepicker popup was closed due to a date being selected?

Is there a way to detect when the mat-datepicker popup closes after a date is selected? I want to perform certain actions only if the user clicked on a date, not if the popup was closed by using the escape key or backdrop click. I am aware of the @Output( ...

What is the most effective way to exchange data among multiple React applications?

I am looking for a solution to securely share data among multiple applications, with some parts of the data being secure and others not. I have explored options like IndexedDB and localStorage, but they don't work in all browsers, especially in incogn ...