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"
}
store. dispatch(logout()); In effects, dispatch logout success action so that the user logs out and the store is reset.
type = 'CLEAR' ONCE, it will clear all the properties of the store.
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.
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.
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.”
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With