Question:
Providing multiple reducers as root ones using provide State() in TypeScript?

Problem

Current version of Angular and ngrx are 16.0.0.


StoreModule.forRoot({

      app: appReducer,

      anotherFeatureName: anotherFeatureReducer

    }),

    EffectsModule.forRoot([

      AppEffects,

      AnotherEffects

    ]),


I started migration to standalone Angular app. So I need to get rid off StoreModule and EffectsModule from app.module.ts. But I can find the way to provide several reducers and relative effects using standalone API's.


Tried different ways to provide but no ones is working as expected. The state is undefined when app tries to read it.


Solution

1. First, make sure you have imported the necessary NgRx modules and your reducers and effects.


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

import { provideEffects } from '@ngrx/effects';

import { appReducer } from './app.reducer';

import { anotherFeatureReducer } from './another-feature.reducer';

import { AppEffects } from './app.effects';

import { AnotherEffects } from './another.effects';


2.In your app module, use the provideState() function to provide your root reducers


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


@NgModule({

  imports: [

    //Remove StoreModule.forRoot()

    //Remove EffectsModule.forRoot()

    // Add provideState() to provide your root reducers

    provideState({

      app: appReducer,

      anotherFeatureName: anotherFeatureReducer,

    }),

  ],

})

export class AppModule {}


3. Similarly, use the provideEffects() function to provide your root effects


import { provideEffects } from '@ngrx/effects';


@NgModule({

  imports: [

    //Remove StoreModule.forRoot()

    //Remove EffectsModule.forRoot()

    // Add provideState() to provide your root reducers

    provideState({

      app: appReducer,

      anotherFeatureName: anotherFeatureReducer,

    }),


    // Add provideEffects() to provide your root effects

    provideEffects([AppEffects, AnotherEffects]),

  ],

})

export class AppModule {}


Suggested blogs:

>Fix: Strange compilation error with Typescript and Angular

>What if the property 'X' does not exist on type 'FastifyContext<unknown>'?

>How to get the type of a class property's getter/setter in TypeScript?

>How to assign multiple const variables in a single declaration in TypeScript?

>Type inference with 'as const' IN TypeScript

>Typescript Return argument: Tuple type or passed to rest parameter warning?

>Why Typescript does not allow a union type in an array?

>Narrow SomeType vs SomeType[]

>Create a function that convert a dynamic Json to a formula in Typescript

>How to destroy a custom object within the use Effect hook in TypeScript?

>How to type the values of an object into a spreadsheet in TypeScript?


Nisha Patel

Nisha Patel

Submit
0 Answers