Consider this Angular component that selects a piece of state from the NgRx store and displays it.
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
@Component({
selector: 'app-counter',
template: `Count: {{ count$ | async }}`
})
export class CounterComponent {
count$: Observable;
constructor(private store: Store<{ count: number }>) {
this.count$ = this.store.select('count');
}
} If the store's state is { count: 5 }, what will this component render?
Think about what the select method does and how the async pipe works.
The store.select('count') returns an observable of the count value from the store. The async pipe subscribes to this observable and displays the current value. Since the store state has count: 5, the component renders Count: 5.
Given this reducer function for a counter feature:
import { createReducer, on } from '@ngrx/store';
import { increment, decrement } from './counter.actions';
export const initialState = 0;
export const counterReducer = createReducer(
initialState,
on(increment, state => state + 1),
on(decrement, state => state - 1)
);If the current state is 3 and the increment action is dispatched, what will be the new state?
Look at how the reducer updates the state when the increment action is received.
The reducer adds 1 to the current state when the increment action is dispatched. Starting from 3, the new state becomes 4.
Which of the following code snippets correctly defines an NgRx action named addItem that carries a payload of type string?
Remember that NgRx actions with payloads use props to define the payload shape.
Option D correctly uses createAction with props<{ item: string }>() to define the payload. Other options either misuse the syntax or omit props.
Given this selector code:
import { createSelector } from '@ngrx/store';
const selectFeature = (state: any) => state.feature;
const selectItems = createSelector(
selectFeature,
(feature) => feature.items
);When the store state is {} (empty object), selecting selectItems causes an error. Why?
Think about what happens if state.feature does not exist.
If state.feature is undefined, then feature.items tries to access a property on undefined, causing a TypeError at runtime.
Choose the best explanation for why developers use NgRx store in Angular apps.
Think about how NgRx helps manage state and debugging.
NgRx store centralizes state in one place and enforces immutability, which helps developers track changes and debug easily. Other options describe unrelated or incorrect features.