Why the UI did not update
Most LWC traps come from reactivity (references vs mutations), timing (wire and lifecycle), naming rules and shadow DOM. Explain the rule and show the one-line fix.
- ReactivityMutation vs reassignment and @api objects.
- TimingWire provisioning and renderedCallback.
- RulesEvent names, $ parameters and shadow DOM.
“Why doesn't my component update?” is the question behind most LWC bugs — and most tricky interview questions. Each scenario below shows the code, what actually happens, and the rule that explains it.
1. Why does the list not re-render? (mutating an array)
Without @track, pushing into an array does not trigger a re-render because the field reference has not changed. Assign a new array (or use @track).
items = [];
addItem(item) {
this.items.push(item); // UI may not update
this.items = [...this.items, item]; // correct: new reference
}2. Why is this.account.data undefined in connectedCallback?
Wire adapters provision data asynchronously; when connectedCallback runs, the first result may not have arrived. Read wired data in getters or a wired function, not in connectedCallback.
3. What happens? (setting a property inside renderedCallback)
Setting a reactive property in renderedCallback triggers another render, which calls renderedCallback again — an infinite loop or unnecessary renders. Guard with a flag or move the logic elsewhere.
renderedCallback() {
this.count = this.count + 1; // re-render → renderedCallback → ... loop
}4. Why does the parent never receive this event?
The event name contains uppercase letters and a hyphen (itemSelected / item-selected) and the parent listens with onitemselected. Event names must be lowercase with no hyphens; the listener is on + the exact name.
this.dispatchEvent(new CustomEvent('itemSelected')); // wrong
this.dispatchEvent(new CustomEvent('itemselected')); // right → onitemselected5. What error do you get? (child changes an @api object)
An “invalid mutation” error — objects passed down through @api are read-only to the child. Copy the value into a local field before editing.
@api config;
handleChange() {
this.config.theme = 'dark'; // error: cannot mutate
this.localConfig = { ...this.config, theme: 'dark' }; // correct
}6. Why is this undefined inside the setTimeout callback?
A regular function callback gets its own this. Use an arrow function, which keeps the component's this.
setTimeout(function () { this.isLoading = false; }, 500); // this is undefined
setTimeout(() => { this.isLoading = false; }, 500); // correct7. Why does the wired Apex call never fire?
The $ prefix is missing, so recordId is passed as a literal undefined at load time and never re-evaluated, or the Apex method lacks cacheable=true. Reactive parameters must be strings starting with $.
@wire(openCases, { accountId: recordId }) // wrong
@wire(openCases, { accountId: '$recordId' }) // right8. Why does querySelector return null for a child component's button?
Shadow DOM encapsulation: this.template.querySelector only searches your own template, not inside child components. Expose an @api method on the child or use events instead of reaching in.
Remember
Key points
- Reassign, do not mutate
New references trigger renders.
- Wire is asynchronous
Read data in getters.
- Lowercase event names
No hyphens, no capitals.
- Arrow functions keep this
In timers and promises.
Interview drill
Follow-up questions
What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.
-
1 Does changing a getter's dependency re-render the template? Basic
Yes — if the getter reads reactive fields, the template re-evaluates it when they change.
-
2 Can you use async/await in connectedCallback? Intermediate
Yes, mark it async; the framework does not wait for it, so show a loading state until data arrives.
-
3 Why does my CSS not style a child component? Intermediate
Shadow DOM blocks styles from leaking into children; use styling hooks, slots or CSS custom properties exposed by the child.
-
4 Why does an @api setter not run when I mutate the object in the parent? Intermediate
Setters run on assignment of a new value; mutating the same object does not reassign it. Pass a new object.
-
5 Why can't I call alert() in LWC? Basic
Native alert and confirm are blocked in cross-origin iframes and not recommended; use LightningAlert and LightningConfirm.
-
6 Why does Jest show stale DOM after setting a property? Intermediate
Re-rendering is asynchronous; await Promise.resolve() (or flush promises) before asserting.