>

CODEMASTERYLAB

IDE PLATFORM
CoursesCommunity BlogsKnowledge GraphQuizzesInterview Prep

Sign in to track progress & earn XP

Sign In Create Account
>
Code Mastery Lab

Code Mastery Lab

Sign In

Redux in Angular — Connecting the Store to Components

Question 1 / 1

0:00
Core
Medium

A ProfileComponent injects the Redux store and calls this.readState() only inside the store.subscribe() callback. On initial render, the template shows no user data even though the store already has a user in state. What is the cause?

typescript export class ProfileComponent implements OnInit, OnDestroy { username: string; private unsubscribe: () => void;

constructor(@Inject(AppStore) private store: Store<AppState>) {}

ngOnInit(): void { this.unsubscribe = this.store.subscribe(() => this.readState()); // readState() never called manually }

readState(): void { this.username = this.store.getState().user.currentUser?.name; }

ngOnDestroy(): void { this.unsubscribe(); } }