0
0
Angularframework~20 mins

Effects for side effects in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NgRx Effects Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an effect dispatches an action?

Consider an Angular effect that listens for an action and dispatches another action. What is the result in the application?

Angular
import { createEffect, ofType, Actions } from '@ngrx/effects';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import * as MyActions from './actions';

@Injectable()
export class MyEffects {
  effect$ = createEffect(() => this.actions$.pipe(
    ofType(MyActions.loadData),
    map(() => MyActions.loadDataSuccess({ data: [1, 2, 3] }))
  ));

  constructor(private actions$: Actions) {}
}
AThe effect listens for 'loadData' action and dispatches 'loadDataSuccess' action, updating the store accordingly.
BThe effect listens for 'loadData' but does not dispatch any action, so the store remains unchanged.
CThe effect causes an infinite loop because it dispatches the same action it listens for.
DThe effect runs only once and then stops listening for actions.
Attempts:
2 left
💡 Hint

Think about what happens when an effect returns a new action.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this effect definition

Which option shows the correct syntax for creating an effect that does NOT dispatch an action?

Angular
import { createEffect, ofType, Actions } from '@ngrx/effects';
import { Injectable } from '@angular/core';
import { tap } from 'rxjs/operators';
import * as MyActions from './actions';

@Injectable()
export class MyEffects {
  logEffect$ = createEffect(() => this.actions$.pipe(
    ofType(MyActions.loadData),
    tap(() => console.log('Load data action received'))
  ), { dispatch: false });

  constructor(private actions$: Actions) {}
}
AThe effect is missing a return statement inside createEffect.
BThe effect should use map instead of tap to dispatch an action.
CThe effect must include { dispatch: false } to avoid dispatching an action.
DThe effect should not use ofType operator.
Attempts:
2 left
💡 Hint

Think about how to create effects that only perform side effects without dispatching new actions.

🔧 Debug
advanced
2:00remaining
Why does this effect cause an infinite loop?

Examine the effect below. Why does it cause an infinite loop?

Angular
import { createEffect, ofType, Actions } from '@ngrx/effects';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import * as MyActions from './actions';

@Injectable()
export class MyEffects {
  loopEffect$ = createEffect(() => this.actions$.pipe(
    ofType(MyActions.loadData),
    map(() => MyActions.loadData())
  ));

  constructor(private actions$: Actions) {}
}
ABecause the effect uses map instead of tap, causing a syntax error.
BBecause the effect listens for 'loadData' and dispatches the same 'loadData' action, triggering itself repeatedly.
CBecause the effect does not unsubscribe from the actions stream.
DBecause the effect is missing { dispatch: false } option.
Attempts:
2 left
💡 Hint

Think about what happens when an effect dispatches the same action it listens for.

state_output
advanced
2:00remaining
What is the state after this effect runs?

Given the reducer and effect below, what will be the state after dispatching 'loadData'?

Angular
import { createReducer, on } from '@ngrx/store';
import * as MyActions from './actions';

export interface State {
  data: number[];
  loading: boolean;
}

export const initialState: State = {
  data: [],
  loading: false
};

export const myReducer = createReducer(
  initialState,
  on(MyActions.loadData, state => ({ ...state, loading: true })),
  on(MyActions.loadDataSuccess, (state, { data }) => ({ ...state, data, loading: false }))
);

// Effect
import { createEffect, ofType, Actions } from '@ngrx/effects';
import { Injectable } from '@angular/core';
import { map, delay } from 'rxjs/operators';

@Injectable()
export class MyEffects {
  loadData$ = createEffect(() => this.actions$.pipe(
    ofType(MyActions.loadData),
    delay(1000),
    map(() => MyActions.loadDataSuccess({ data: [10, 20, 30] }))
  ));

  constructor(private actions$: Actions) {}
}
A{ data: [], loading: false } immediately after dispatch
B{ data: [], loading: true } immediately after dispatch
C{ data: [10, 20, 30], loading: true } immediately after dispatch
D{ data: [10, 20, 30], loading: false } after 1 second delay
Attempts:
2 left
💡 Hint

Consider the timing of the effect and reducer updates.

🧠 Conceptual
expert
2:00remaining
Why use effects instead of components for side effects?

In Angular with NgRx, why is it better to handle side effects like HTTP calls inside effects rather than inside components?

AEffects centralize side effects, keep components simple, and allow better testing and state management.
BComponents cannot perform HTTP calls directly, so effects are required.
CEffects run on the server only, improving performance.
DUsing effects avoids the need for reducers in state management.
Attempts:
2 left
💡 Hint

Think about separation of concerns and testability.