Angular

Basic State Management

Component state, services state, BehaviorSubject, state patterns, stateful vs stateless components, shared state

20 interview questionsยท
Mid-Level
1

What is the simplest way to declare local state in an Angular component?

Answer

Declaring a class property directly in the component is the simplest method for managing local state. This approach is perfect for data that is only used by this component and doesn't need to be shared. Properties are automatically accessible in the template and can be modified directly.

2

What is the main characteristic of a stateless component?

Answer

A stateless component has no internal state and relies solely on @Input to receive its data. These components are purely presentational and emit events via @Output to communicate with their parent. This architecture improves reusability and facilitates testing.

3

Why use a BehaviorSubject instead of a Subject for state management?

Answer

BehaviorSubject always stores the last emitted value and immediately sends it to new subscribers. This characteristic is essential for state management because components need access to the current state as soon as they initialize, without waiting for a new emission. Subject keeps no value and new subscribers only receive future emissions.

4

What is the best practice for exposing state managed by an Angular service?

5

How to share state between multiple components that are not related by a parent-child relationship?

+17 interview questions

Master Angular for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free