# How does conditional rendering work in LWC templates using modern directives?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Conditional Rendering, Templates

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-conditional-rendering-work-in-lwc-templates-using-modern-directives

## Answer

**Basics:** Conditional rendering decides which parts of the template appear. For example, show a spinner while loading, an error panel if a call failed, and content otherwise. The condition must be a simple property or getter, because templates do not allow expressions.

**How it works:**
1. `<template lwc:if={isLoading}>...</template>` renders when the value is truthy.
2. Immediately following sibling: `<template lwc:elseif={hasError}>...</template>`.
3. Final fallback: `<template lwc:else>...</template>`.
4. The branches must be adjacent siblings; no other element can sit between them.
5. Complex logic goes into a getter in JS, such as `get showEmpty() { return !this.isLoading && !this.rows.length; }`.

**Advanced / deep dive:** `lwc:if` (Spring '23 and later) is the supported directive; `if:true` and `if:false` still work but are legacy and cannot express else-if chains, so migrate when touching the code. Do not mix legacy and new directives on the same chain. When a branch is false, its DOM and child components are destroyed; when it becomes true they are created again, firing child `connectedCallback` and losing child state and unsaved input. If you need to preserve state or avoid recreation cost, hide with a CSS class like `slds-hide` instead. Removing large subtrees repeatedly is costly, so for frequently toggled sections consider hiding. Also remember conditionals interact with `renderedCallback`: elements in a false branch will be null when you query them. Evaluate order matters: the first truthy branch wins.

**Code / config example:**
```html
<template lwc:if={isLoading}>
    <lightning-spinner alternative-text="Loading"></lightning-spinner>
</template>
<template lwc:elseif={hasError}>
    <p class="slds-text-color_error">{errorMessage}</p>
</template>
<template lwc:else>
    <c-results rows={rows}></c-results>
</template>
```
```javascript
get hasError() { return !!this.error; }
get errorMessage() { return this.error?.body?.message ?? 'Unknown error'; }
```

**Hidden gotchas & must-remember points:**
- You cannot write expressions like `{!isLoading}` or `{a && b}` in the template; use getters.
- `lwc:elseif` and `lwc:else` must immediately follow the previous branch.
- Toggling destroys and recreates child components, losing their state.
- `if:true`/`if:false` are legacy; new code should use `lwc:if`.
- Values are evaluated for truthiness, so `0`, `''`, `null` and `undefined` count as false.
- Queries for elements inside a false branch return null.

**Best practice:** Use `lwc:if` chains with getters for readable logic, and hide with CSS when state must survive toggling.

---
Source: ForceChallenges · last updated 2026-11-02
