I'm trying to figure out how to set a specific value in the latest version of Ngrx. The docs mention how to increment/decrement/reset values in the store, but I didn't see any examples on how to dynamically set values or how to pass arguments to reducers.
This is what I have at the moment, but I know it's not correct:
My actions:
// TODO: properly implement action
export const setLabel = createAction('[Label Component] Set, props<{ addressField: string }>()');
My reducer:
export interface AppState {
addressField;
}
const _reducer = createReducer(
// initial state:
{ addressField: '' },
// TODO: update `addressField`:
on(setLabel, state => {
return {
...state
};
})
);
export function labelReducer(state, action) {
return _reducer(state, action);
}
Finally, my component:
// imports...
export class MyComponent implements OnInit {
constructor( private store: Store<AppState>,
private AddressService: AddressService) {
}
ngOnInit() {
// TODO: update store state:
this.AddressService.getFields().subscribe(x => {
this.store.dispatch(setLabel({ addressField: x.addressLine }));
});
}
}
actions.ts
export enum ActionTypes {
SetLabel = '[Label Component] Set'
}
export const SetLabel = createAction(ActionTypes.SetLabel, props<{ addressField: string }>());
reducer.ts
export interface AppState {
addressField;
}
export initialState: AppState = {
addressField: ''
}
const _reducer = createReducer(
initialState,
on(SetLabel, (state, { addressField }) => {
return {
...state,
addressField
};
})
);
Your component is fine, better to use Effects when dealing with Side Effects (async data)