Having trouble getting custom NgRx Actions to work properly in Angular 11?

I'm in the process of building a simple application to enhance my understanding of NgRx. Here's the code I've come up with, but it's not functioning as expected.

Here's my Action.ts:

import { Action } from '@ngrx/store';
import { Employee } from '../Employee.model';

export const ADD_EMPLOYEE = 'ADD_EMPLOYEE';
export const EDIT_EMPLOYEE = 'EDIT_EMPLOYEE';


export class AddEmployee implements Action {
    readonly type = ADD_EMPLOYEE;
    constructor(public payload: Employee){}
}

export class EditEmployee implements Action {
    readonly type = EDIT_EMPLOYEE;
    // constructor(public payload: Employee){}
}

export type EmployeeAction = EditEmployee | AddEmployee;

Now for my reducer.ts

import * as EmployeeActions from './employee.actions';

const initialState = {
    states: [
        {name: 'Arizona', code: 'Arizona'},
        {name: 'California', value: 'California'},
        {name: 'Florida', code: 'Florida'},
        {name: 'Ohio', code: 'Ohio'},
        {name: 'Washington', code: 'Washington'}
    ],
    
};
export function employeeReducer (state = initialState, action: EmployeeActions.EmployeeAction) {

    switch(action.type) {
        case EmployeeActions.ADD_EMPLOYEE:
            console.log("Employee reducer called: ", action.payload);
            return {
                ...state,
                
            };
        default:
            return state;

    }
}

And here's my app.module.ts:

import { StoreModule } from '@ngrx/store';
.....

imports: [
    BrowserModule,
    BrowserAnimationsModule,
    StoreModule.forRoot({employeeStoreReducer: employeeReducer})
  ],

However, I'm encountering the following error at this point:

error TS2322: Type '(state: { states: ({ name: string; code: string; value?: undefined; } | { name: string; value: string; code?: undefined; })[]; } | undefined, action: EmployeeAction) => { states: ({ name: string; code: string; value?: undefined; } | { ...; })[]; }' is not assignable to type 'ActionReducer<{ states: ({ name: string; code: string; value?: undefined; } | { name: string; value: string; code?: undefined; })[]; }, Action>'.
  Types of parameters 'action' and 'action' are incompatible.
    Type 'Action' is not assignable to type 'EmployeeAction'.
      Property 'payload' is missing in type 'Action' but required in type 'AddEmployee'.

36     StoreModule.forRoot({employeeStoreReducer: employeeReducer}),
                           
  src/app/employee/store/employee.actions.ts:10:17
    10     constructor(public payload: Employee){}

    'payload' is declared here.

If I change action: EmployeeActions.EmployeeAction to action: Action, it starts working. However, in that case, I can't send any payload. Can someone please assist me in resolving this issue? Additionally, please explain the reason behind this discrepancy. If necessary, I can provide more code. I'm currently using Angular version 11.1.2 and NgRx version 11.0.1. Thank you in advance.

Answer ā„–1

If you're working on a project that utilizes Angular/NGRX v11.x and are eager to explore modern NGRX techniques, consider abandoning your current pattern and opt for the factory functions offered by the library.

Here is an example:

Action File

export const addEmployee = createAction(
  ADD_EMPLOYEE, 
  props<{payload: Employee}>()
);
export const editEmployee = createAction(
  EDIT_EMPLOYEE, 
  props<{payload: Employee}>()
);

Reducer File

export const reducer = createReducer(
  initialState,
  
  on(EmployeeActions.addEmployee, (state, { payload }) => {
    console.log("Employee Reducer Called")
    return {
       ...state, //Insert Add Logic here
    };
  }),

  on(EmployeeActions.editEmployee, (state, { payload }) => {
    console.log("Employee Reducer Called")
    return {
       ...state, //Insert Edit Logic here
    };
  }),
);

The NGRX Docs illustrate this concept effectively:

Answer ā„–2

Considering that you are utilizing an updated edition of ngrx, the parameter should take on the form of an Action.

To proceed, establish your reducer and actions as outlined in this documentation. The specific details for creating the actions are provided just above.

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

Troubleshooting a unique Flex layout problem in Angular 2 with Angular Material

Can anyone help me figure out how to recreate this design using Flex within my Angular2 materialDesign stack? https://i.stack.imgur.com/le1ld.png Currently, I'm facing an issue where blocks 2 and 3 keep ending up below block 4... https://i.stack.im ...

What is the best method for exporting Angular data to Excel while preserving cell formatting?

Here is the code for exporting data from Excel: public exportDataToExcel(jsonData: any[], fileName: string): void { const workSheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(jsonData); const workBook: XLSX.WorkBook = { Sheets: { 'data&ap ...

a dedicated TypeScript interface for a particular JSON schema

I am pondering, how can I generate a TypeScript interface for JSON data like this: "Cities": { "NY": ["New York", [8000, 134]], "LA": ["Los Angeles", [4000, 97]], } I'm uncertain about how to handle these nested arrays and u ...

Ways to deactivate HTML Angular attribute recommendations in Visual Studio 2019?

I am looking for a way to get rid of the Angular suggestions in my html files within Visual Studio 2019, as I do not use Angular. https://i.sstatic.net/lsP0y.jpg Can anyone point me in the right direction? I attempted to locate it under Tools -> Option ...

Typescript: Variable of unspecified type

Within my generator function called foo(), I am making a request to an external API using the fetch method. Once I receive the response data from the API, I then parse it as JSON. Encountering an issue, as Typescript is triggering an error message: An Obj ...

Issues with HttpTestingController's expectOne method arise when the actual request is contained within a promise

Currently, I am in the process of creating a unit test for a service that involves retrieving data from another service (which returns a Promise) and then making an HTTP GET request. To mock the HTTP request, I am utilizing HttpTestingController from @ang ...

Issues with Webpack and TypeScript CommonsChunkPlugin functionality not functioning as expected

Despite following various tutorials on setting up CommonsChunkPlugin, I am unable to get it to work. I have also gone through the existing posts related to this issue without any success. In my project, I have three TypeScript files that require the same ...

What is the best way to verify nested objects with a generic type?

Consider a scenario where I have a generic type defined as follows: type Item<T> = {a:T; B:T} In this case, I aim to automatically determine an object with consistent fields types without explicitly stating the generic type: const items: Record<s ...

Steps to simulate a TouchEvent programmatically using TypeScript

Is there a way to manually trigger the touch event in TypeScript? In JavaScript, it was possible but I am struggling to achieve the same in TypeScript. For example: let touchStart: TouchEvent = document.createEvent('TouchEvent'); touchStart.i ...

What is the best way to attach a DOM element to an Angular 2 TestBed?

I was wondering if it is possible to add a DOM element to the test DOM before running tests. I'm new to testing, but something like fixture.debugElement.prepend('div') might work. However, I understand that this approach may indicate a desig ...

Be patient for the Observable to finish its execution within another Observable

I have a data structure that consists of two classes: Repo and Contributor. export class Repo { id: number; name: string; contributors: Contributor[]; } export class Contributor { id: number; login: string; } Currently, I am using Ob ...

Encountering issues with installing Angular using npm due to errors

When I run npm install, I encounter errors. Though I can get it to work by using npm install --force, I prefer not to rely on the force flag. After reading through the errors, it seems like there might be a version compatibility issue, but I'm having ...

What is the method to confirm if my element has received focus in an Angular 4 unit test?

I have the following function that needs to be unit tested. I am accessing a text box element using ViewChild in my component, and during testing I want to verify whether the text box receives focus after calling setTimeout(). @ViewChild('searchInpu ...

Putting together Angular2-jwt, refreshing tokens, and making use of AuthHttp

It feels like Iā€™m going in circles here, maybe because of using so many subscriptions and having to chain them together. I need to be able to refresh a token if it expires using the refresh token. Can someone provide a simple working example for this? ...

Exploring Angular's ngFor feature in building a dynamic accordion menu that automatically closes one panel and opens another when clicked

The Angular Material accordion component is a key feature in my Angular project. Utilizing an ngFor loop to iterate through the information stored in menuItems, I dynamically create a new expansion panel for each item. With two components in play, I seaml ...

What is the best way to retrieve and showcase data in NextJs version 13 and beyond?

Being new to NextJS, my question may seem trivial but I'd appreciate your patience. Essentially, my goal is to fetch data from a database and display it on the page upon the initial render. To achieve this, I am utilizing the useEffect and useState ho ...

When you use array[index] in a Reduce function, the error message "Property 'value' is not defined in type 'A| B| C|D'" might be displayed

Recently, I delved deep into TypeScript and faced a challenge while utilizing Promise.allSettled. My objective is to concurrently fetch multiple weather data components (such as hourly forecast, daily forecast, air pollution, UV index, and current weather ...

What is the best way to toggle the Angular date picker on and off for a specific date with Angular Material?

I need to display the start date and end date where the start date is in format dd/mm/yyyy, for example 10/09/2020, and the end date should be yesterday's date, i.e., 09/09/2020. All other dates should be disabled. What steps should I take to impleme ...

Compiling Typescript upon saving in Sublime Text 3

Seeking a solution: How can I get Sublime Text 3 to automatically compile Typescript code when I save it? Having to switch between the terminal and Sublime is getting tedious. Appreciate any advice, thank you! ...

CSS files loaded from an external source onto Google Chrome browser are not being properly

There seems to be an issue with my external CSS style not reflecting in Chrome, even though it is downloaded. When I manually change the stylesheet inline, it works but not otherwise. Interestingly, it functions fine in the Firefox browser. Here is how I ...