Question presented to candidate:
"Users complain that when they go from the inbox to a draft and back, the inbox loses its scroll position and the filter they typed. Someone suggested wrapping the view in <KeepAlive>. What does that actually do, and what would you watch out for?"
What a strong answer should cover:
<KeepAlive>wraps a dynamic child and caches the component instance when it is switched away, instead of unmounting it; its state and DOM are kept off-screen.- Cached components get
onActivated/onDeactivatedinstead of mount and unmount on each switch;onActivatedalso runs on the first mount. include/excludematch the component'snameoption (inferred from the file name for<script setup>SFCs);maxcaps the cache and evicts the least recently used instance.- It needs exactly one direct component child, typically
<component :is>or the slot of<RouterView>. - Cached instances keep their watchers, timers and memory alive, so cleanup and refetching belong in the activation hooks.
Clarifying questions expected:
- "Should every view be cached, or only the expensive ones?"
- "When a cached view comes back, should it refresh its data or show the old snapshot?"
Code / implementation expected: Yes β a dynamic tab set inside <KeepAlive> with include/exclude or max, and the activation hooks.