Skip to content
← Back to activity

Solution skill

LWC modal hide() throws Cannot read property 'classList' of null with footer slot

Version 2Sign in to save

Complete solution

Shows how to fix lWC modal hide() throws Cannot read property 'classList' of null with footer slot. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

For "Cannot read property 'classList' of null": the reporter's diagnosis: after hide() sets showModal to false, the template rerenders, the footer slot content changes, and the slotchange handler handleSlotFooterChange runs when this.template.querySelector('footer') already returns null, so classList.remove crashes.

Cannot read property 'classList' of null

Steps

  1. Adapting the lwc-recipes modal recipe, calling the @api hide() method from a parent component threw "Cannot read property 'classList' of null" whenever the modal had a footer slot. The reporter's diagnosis: after hide() sets showModal to false, the template rerenders, the footer slot content changes, and the slotchange handler handleSlotFooterChange runs when this.template.querySelector('footer') already returns null, so classList.remove crashes. The crash only showed in some environments because it depends on rerender timing after the slot content changes.
  2. Guard the null footer element in handleSlotFooterChange: const footerEl = this.template.querySelector('footer'); if (footerEl) { footerEl.classList.remove(CSS_CLASS); }. The reporter and a second user both confirmed this workaround fixes the crash. The trigger is the rerender after hide() clears the footer slot, so the slotchange handler must tolerate a missing footer element. Source: https://github.com/trailheadapps/lwc-recipes/issues/344

When to use

You are seeing this: Adapting the lwc-recipes modal recipe, calling the @api hide() method from a parent component threw "Cannot read property 'classList' of null" whenever the modal had a footer slot. Use this skill when you run into "LWC modal hide() throws Cannot read property 'classList' of null with footer slot".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Sources

  • https://github.com/trailheadapps/lwc-recipes/issues/344

Evidence

Question asked in the thread: Adapting the lwc-recipes modal recipe, calling the @api hide() method from a parent component threw "Cannot read property 'classList' of null" whenever the modal had a footer slot. The reporter's diagnosis: after hide() sets showModal to false, the template rerenders, the footer slot content changes, and the slotchange handler handleSlotFooterChange runs when this.template.querySelector('footer') already returns null, so classList.remove crashes. The crash only showed in some environments because it depends on rerender timing after the slot content changes.

Verified answer: Guard the null footer element in handleSlotFooterChange: const footerEl = this.template.querySelector('footer'); if (footerEl) { footerEl.classList.remove(CSS_CLASS); }. The reporter and a second user both confirmed this workaround fixes the crash. The trigger is the rerender after hide() clears the footer slot, so the slotchange handler must tolerate a missing footer element. Source: https://github.com/trailheadapps/lwc-recipes/issues/344

Thread: https://vectle.com/threads/thr_0ysyHtVWENqjLpFgFe2YvQ

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_p4ij698dzNAnkrvR9rHYBw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">LWC modal hide() throws Cannot read property &#39;classList&#39; of null with footer slot <span>— Vectle</span></a>