The right technique depends on how the components are related. The examples below use Angular and React, the two most common cases. 1. Parent to child: inputs / props - Angular: @Input() (or signal input()) — <app-order-card [order]="selectedOrder" /> - React: props — <OrderCard order={selectedOrder} /> 2. Child to parent: outputs / callback props - Angular: @Output() selected = new EventEmitter<Order>(); (or output()) — <app-order-card (selected)="onSelect($event)" /> - React: pass a callback — <OrderCard onSelect={handleSelect} />, and the child calls onSelect(order). 3. Siblings or distant components: shared state - Angular: a shared service (providedIn: 'root' or provided at a feature level) holding state as signals or a BehaviorSubject. @Injectable({ providedIn: 'root' }) export class SelectionService { readonly selectedOrder = signal<Order | null>(null); } - React: lift state to the nearest common parent, or use Context for widely needed data. - Larger applications: state management libraries (NgRx or NgRx SignalStore; Redux Toolkit, Zustand or Jotai). 4. Through the URL: route parameters and query strings - /orders/42?tab=history — any component can read the route. Good for state that should survive refresh and be shareable (filters, selected item, pagination). 5. Template references and view queries (Angular) - A parent can access a child component instance with @ViewChild or viewChild() for direct calls (use sparingly). 6. Content projection / children - Pass ready-built UI into a component (<ng-content> in Angular, children in React), avoiding prop drilling for layout composition. 7. Server or cache state - Data fetched from APIs shared through caching libraries (TanStack Query, RTK Query, Angular services with caching), so components request the same data without passing it around. 8. Browser storage and events (special cases) - localStorage or sessionStorage for persistence across reloads; BroadcastChannel or storage events for communication between tabs; custom DOM events between micro-frontends. Choosing - Use the simplest mechanism that fits: inputs and outputs for direct relationships, a service or context for shared state, the URL for navigational state, and a store only when complexity justifies it. Keep data flow predictable: one owner per piece of state, and avoid two-way coupling between distant components.
Ravindra Wadile · · 0 likes