hardFrontend

What is NgRx?

675 views
01

Understand the problem

Explain NgRx state management.

ngrxstate
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Reducer + selector (sketch)
import { createReducer, on, createAction, props, createSelector } from '@ngrx/store';

export const add = createAction('[Cart] Add', props<{ item: string }>());

export const cartReducer = createReducer(
  [] as string[],
  on(add, (state, { item }) => [...state, item]),   // pure, returns NEW state
);

// Memoized selector to read a slice of the store:
export const selectCart = (s: { cart: string[] }) => s.cart;
export const selectCount = createSelector(selectCart, (cart) => cart.length);

// In a component: this.store.dispatch(add({ item: 'book' }));
//                 count$ = this.store.select(selectCount);
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.