Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NgRx createReducer() and on() is giving error

I am new. to NgRx.

When I am trying to create reducer using createReducer() I am getting error Expected 4 arguments, but got 2. When I am trying to pass 3rd and 4th argument as null, I am getting error

Argument of type '(state: any, { updatedValue }: any) => any' is not assignable to parameter of type 'readonly ActionCreator<string, FunctionWithParametersType<any[], object>>[]'.
  Type '(state: any, { updatedValue }: any) => any' is missing the following properties from type 'readonly ActionCreator<string, FunctionWithParametersType<any[], object>>[]': concat, join, slice, indexOf, and 15 more.ts(2345)

Reducer code

import { createReducer, on, State, Action } from '@ngrx/store';

import { Ingredient } from '../../shared/ingredient.model';
import * as ShoppingListAction from './shopping-list.action';

export const initialState = {
  ingredients: [
    new Ingredient('Apples', 5),
    new Ingredient('Tomatoes', 10),
  ]
}

export const shoppingListReducer = createReducer(
  initialState,
  on(
    'ADD_INGREDIENT',
    (state: any, { updatedValue }: any) => ({ ...state, prop: updatedValue }),
    null,
    null
  )
);

actions code

import { createAction, props } from '@ngrx/store';
import { Ingredient } from '../../shared/ingredient.model';


export const ADD_INGREDIENT = 'ADD_INGREDIENT';

export const AddIngredient = createAction(
  ADD_INGREDIENT,
  props<Ingredient>()
);

app.module.ts

import { SharedModule } from './shared/shared.module';
import { CoreModule } from './core.module';
import { LoggingService } from './logging.service';

// Store
import { shoppingListReducer } from './shopping-list/store/shopping-list.reducer';

@NgModule({
  declarations: [AppComponent, HeaderComponent],
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule,
    SharedModule,
    CoreModule,
    StoreModule.forRoot({ 'slReduce': shoppingListReducer })
  ],
  bootstrap: [AppComponent],
  // providers: [LoggingService]
})
export class AppModule {}

package.json

"dependencies": {
    "@angular/animations": "^11.2.1",
    "@angular/common": "^11.2.1",
    "@angular/compiler": "^11.2.1",
    "@angular/core": "^11.2.1",
    "@angular/forms": "^11.2.1",
    "@angular/platform-browser": "^11.2.1",
    "@angular/platform-browser-dynamic": "^11.2.1",
    "@angular/router": "^11.2.1",
    "@ngrx/store": "^11.0.1",
    "bootstrap": "3.3.7",
    "core-js": "^3.1.2",
    "rxjs": "^6.0.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.11.3"
  }
like image 525
Piyush Jain Avatar asked Feb 22 '21 06:02

Piyush Jain


People also ask

How do I reset my NgRx?

store. dispatch(logout()); In effects, dispatch logout success action so that the user logs out and the store is reset.

How do I clean my NgRx store?

type = 'CLEAR' ONCE, it will clear all the properties of the store.

Why is NgRx immutable?

Because each action returns a new state (instead of mutating the previous one) ngrx can keep a record of all the previous states for debugging purposes. That wouldn't be possible if you were mutating the same object in memory.

What is the CreateAction creator function in ngrx?

The createAction creator function opened opportunities in the NgRx world. With it came two other creator functions, createReducer and createEffect. Let's take a look at what's so special about it and why it's important. In previous versions of NgRx, defining a new action took 3 steps.

What are ngrx effects in Redux state?

One thing that took a while for us to get our heads around is NgRx Effects. It’s how you deal with asynchronous actions in Redux state; in short, it gives you a hook into your Redux store to say “every time an event is dispatched, after it’s been reduced, let me know.”

What is the use of createreducer function?

Instead of a good old switch statement, createReducer uses on functions to handle the actions and reduce a new state. The on function expects at least one ActionCreator, and the last argument is an action reducer. An action reducer can be compared to a normal reducer function.

What is the difference between ngrx action creator and oftype?

That’s another advantage that the Action Creator brings — it doesn’t need that generic. ofType that takes Action Creator is released with NgRx version 8.0.0. With Action Creator in place and ofType adjusted to handle it better, the team focused on implementing a createReducer function, instead of going through a switch statements.


1 Answers

I was seeing this error in my IDE, along with some other strange type-related errors with @ngrx/store, but the projects were compiling just fine.

I had upgraded my typescript version in my projects that use @ngrx to the latest (v4.3.4 at the time), but my workspace included several other projects, and my IDE (I'm using WebStorm) typescript language service was configured to use an older version of typescript that was installed on one of my other projects. After configuring the IDE typescript service to use the newer typescript version, the errors went away.

So in my case, it was a couple different things - first, I needed to upgrade the typescript version for my projects that use @ngrx to work with some newer language features @ngrx is using. Then, I also had to make sure my IDE's typescript language service was using that version, or at least a fairly recent version of typescript to avoid seeing bogus errors in the editor windows.

like image 58
reads0520 Avatar answered Nov 15 '22 01:11

reads0520