I'm facing NgRx and I can't understand how could be so hard add a value to the store and retrieve it. Anyway, that's what I have done so far
Effects ts:
export class idEffects {
saveId$ = createEffect(() =>
this.actions$
.pipe(
ofType(idActions.addIdAction),
tap(payload => {
console.log("action",payload);
})
)
,{dispatch: false});
constructor(
private actions$: Actions
) {}
}
Actions ts:
export const ADD_ID = '[Id Action] add id';
export const addIdAction = createAction(
ADD_ID,
props<{id: any}>()
)
Reducer ts:
export interface IdState {
id: any;
}
export const defaultId: IdState = {
id: undefined
};
export const idReducer = createReducer (
defaultId,
on(idActions.addIdAction, (state, action) => {
//console.log("Calling id reducer", action);
return {
id: action
}
})
)
and the Selector ts:
export const selectIdState =
createFeatureSelector<IdState>("id")
export const selectIdValue = createSelector(
selectIdState,
id => id
);
Now, that's what I have in my app.module.ts
StoreModule.forRoot({id: idReducer}),
EffectsModule.forRoot([IdEffects]),
StoreModule.forRoot(reducers, {
metaReducers
}),
StoreDevtoolsModule.instrument({maxAge: 25}),
StoreRouterConnectingModule.forRoot({
stateKey: 'router',
routerState: RouterState.Minimal
})
It seems that the storing data is working well because the Redux panel in my console return this:
id(pin):"1"
data(pin):"Hello There"
type(pin):"[Id Action] add id"
But, when I try to retrieve this object I get an undefined
and I do it in this way:
this.id$ = storeId.pipe(select(selectIdValue))
and in the html
<h4>The id obj is: {{id$ | async}}</h4>
But it doesn't write anything. I tried to get the result using a subscribe in the component, and it gets me undefined
how could be possibile?