0
0
Angularframework~20 mins

NgRx store concept in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NgRx Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Angular component using NgRx store?

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?

ACount: 5
BCount: NaN
CCount: 0
DCount: undefined
Attempts:
2 left
💡 Hint

Think about what the select method does and how the async pipe works.

state_output
intermediate
2:00remaining
What is the new state after this NgRx reducer runs?

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?

A4
B3
C-1
D0
Attempts:
2 left
💡 Hint

Look at how the reducer updates the state when the increment action is received.

📝 Syntax
advanced
2:00remaining
Which option correctly defines an NgRx action with a payload?

Which of the following code snippets correctly defines an NgRx action named addItem that carries a payload of type string?

A
import { createAction } from '@ngrx/store';
export const addItem = createAction('addItem', (payload: string));
B
import { createAction } from '@ngrx/store';
export const addItem = createAction('addItem', { item: string });
C
import { createAction, props } from '@ngrx/store';
export const addItem = createAction('addItem', (item: string) =&gt; ({ item }));
D
import { createAction, props } from '@ngrx/store';
export const addItem = createAction('addItem', props&lt;{ item: string }&gt;());
Attempts:
2 left
💡 Hint

Remember that NgRx actions with payloads use props to define the payload shape.

🔧 Debug
advanced
2:00remaining
Why does this NgRx selector cause a runtime error?

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?

ABecause the selector must return an observable, not a value.
BBecause <code>createSelector</code> requires three arguments, not two.
CBecause <code>feature</code> is undefined, accessing <code>feature.items</code> throws a TypeError.
DBecause the state parameter must be typed as <code>any</code> to avoid errors.
Attempts:
2 left
💡 Hint

Think about what happens if state.feature does not exist.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using NgRx store in Angular applications?

Choose the best explanation for why developers use NgRx store in Angular apps.

AIt replaces Angular's dependency injection system for services.
BIt centralizes application state in a single immutable store, making state changes predictable and easier to debug.
CIt automatically generates UI components based on the state shape.
DIt allows direct manipulation of the DOM without Angular templates.
Attempts:
2 left
💡 Hint

Think about how NgRx helps manage state and debugging.