Sub topic 6
Boolean properties and why they should default to false
This chapter assumes you can build a simple Lightning web component, expose a field with @api, and set it from a parent template. It focuses on public properties that hold true or false, and on one rule from the Lightning Web Components (LWC) guidelines: a public Boolean property should default to false.
Why Booleans need special attention
Most public properties take a value. A Boolean property is different, because in markup you can set it by writing only its name. That shortcut is convenient, but it creates a trap. The parent can easily turn a feature on. It cannot easily turn off a feature that the child turned on by default.
Here is the situation in a table.
| Parent markup | Value the child receives |
|---|---|
| `<c-panel>` (attribute absent) | The child's default value |
| `<c-panel expanded>` | `true` |
| `<c-panel expanded={isOpen}>` | The value of `isOpen` in the parent |
| `<c-panel expanded="false">` | The string `"false"`, which is truthy |
The last row is the source of many bugs. It is covered later in this chapter.
A child with a Boolean property
The child below shows a panel body only when expanded is true.
// infoPanel.js (child)
import { LightningElement, api } from 'lwc';
export default class InfoPanel extends LightningElement {
@api heading = '';
@api expanded = false;
}<!-- infoPanel.html (child) -->
<template>
<section>
<h3>{heading}</h3>
<template lwc:if={expanded}>
<p>Details are visible.</p>
</template>
</section>
</template>The parent can now use the property in two ways.
<!-- panelHost.html (parent) -->
<template>
<c-info-panel heading="Shipping" expanded></c-info-panel>
<c-info-panel heading="Billing"></c-info-panel>
<c-info-panel heading="Notes" expanded={notesOpen}></c-info-panel>
</template>// panelHost.js (parent)
import { LightningElement } from 'lwc';
export default class PanelHost extends LightningElement {
notesOpen = false;
}- The first panel receives
true, because the attribute is present with no value. - The second panel receives the default,
false, because the attribute is absent. - The third panel receives whatever
notesOpenholds, and it updates when that value changes.
The rule: default to false
A public Boolean property must not default to true. Suppose the child declared this:
// Problem: the default is true
@api expanded = true;A parent that wants the panel collapsed would have to write one of these:
<c-info-panel expanded="false"></c-info-panel>
<c-info-panel expanded={isFalse}></c-info-panel>The first form does not work. HTML attributes are strings, and in a component template a literal attribute value is passed as a string. The child receives "false", and every non-empty string is truthy in JavaScript. The panel stays expanded. The second form works, but only after you add a field to the parent class whose sole purpose is to hold false. Neither form is natural.
When the default is false, the parent's options are clear:
- Write the attribute name to get
true. - Leave the attribute out to get
false. - Bind to a Boolean expression when the value changes at run time.
The parent never has to write a literal false. This also matches how native HTML Boolean attributes such as disabled and hidden behave, so your components feel familiar to anyone who has written HTML.
Designing the property name for a `false` default
The rule affects how you name things. Name the property for the non-default behavior, so that setting it to true is the unusual choice.
| Avoid (needs a `true` default) | Prefer (works with a `false` default) |
|---|---|
| `@api visible = true` | `@api hidden = false` |
| `@api enabled = true` | `@api disabled = false` |
| `@api showBorder = true` | `@api borderless = false` |
| `@api editable = true` | `@api readOnly = false` |
If you find yourself wanting a true default, ask which behavior most users will want. Then invert the property so that behavior is what you get when the attribute is absent.
Binding Booleans from the parent
Often the parent's value is not a Boolean already. You can use a getter in the parent to produce one.
// taskBoard.js (parent)
import { LightningElement } from 'lwc';
export default class TaskBoard extends LightningElement {
tasks = [];
get hasNoTasks() {
return this.tasks.length === 0;
}
}<!-- taskBoard.html (parent) -->
<template>
<c-info-panel heading="Tasks" expanded={hasNoTasks}></c-info-panel>
</template>Template expressions in LWC cannot contain operators or function calls. That is why the comparison lives in a getter. The getter returns a real Boolean, so the child receives true or false, not a string.
Booleans and one-way data flow
A Boolean public property follows the same one-way data flow rule as any other. The child does not change a value it received. If the child needs to collapse its own panel when the user clicks a button, it should keep its own state and let the parent's value only set the starting point, or it should tell the parent through an event.
// infoPanel.js (child, with its own open state)
import { LightningElement, api } from 'lwc';
export default class InfoPanel extends LightningElement {
@api heading = '';
_expanded = false;
isOpen = false;
@api
get expanded() {
return this._expanded;
}
set expanded(value) {
this._expanded = value === true;
this.isOpen = this._expanded;
}
handleToggle() {
this.isOpen = !this.isOpen;
}
}<!-- infoPanel.html (child, with its own open state) -->
<template>
<section>
<h3>{heading}</h3>
<button onclick={handleToggle}>Toggle</button>
<template lwc:if={isOpen}>
<p>Details are visible.</p>
</template>
</section>
</template>Here the setter copies the parent's value into isOpen, which the child is free to change. The button flips isOpen and never writes to this.expanded. Whenever the parent supplies a new value, the setter runs again and resets isOpen.
The setter uses value === true. This is a deliberate choice, and the next section explains why.
What the child can do about string values
You cannot rely on every parent being careful. A setter can normalize incoming values so that a mistaken "false" does not turn the feature on.
// normalize.js
export function toBoolean(value) {
if (typeof value === 'string') {
return value.toLowerCase() === 'true' || value === '';
}
return Boolean(value);
}// infoPanel.js (child that accepts string input)
import { LightningElement, api } from 'lwc';
import { toBoolean } from './normalize';
export default class InfoPanel extends LightningElement {
_expanded = false;
@api
get expanded() {
return this._expanded;
}
set expanded(value) {
this._expanded = toBoolean(value);
}
}In this version "false" becomes false, an empty string (from a bare attribute in some contexts) becomes true, and real Booleans pass through unchanged. Use this approach only when your component is meant to be consumed in places where string values are likely, such as markup generated by a tool. For ordinary component-to-component use, a false default and a plain @api field is simpler and enough.
Quick diagram of the three cases
Common mistakes
- Defaulting to
true. The parent then has no clean way to turn the feature off. Invert the property name instead. - Passing
"false"as a literal. The string is truthy. Omit the attribute or bind to a Boolean. - Writing the comparison in the template. Expressions such as
{tasks.length === 0}are not allowed. Compute the value in a getter. - Writing to the property inside the child. Assigning to a public property that the parent set can fail or be overwritten on the next update. Copy it into a private field first.
- Using a negated name that confuses readers. A property named
notHiddenis hard to read. Choose the clearest positive form that still defaults tofalse.
Summary of guidance
- Give every public Boolean property a default of
false. - Name the property for the behavior that is off by default.
- In the parent, write the attribute name for
true, omit it forfalse, and bind to a Boolean for dynamic cases. - Never pass a literal
"false". - If the child needs to change the state, copy the value into a private field or send an event to the parent.
Exercise
A child component declares @api highlighted = true;. A parent author writes <c-row highlighted="false"></c-row> and finds the row is still highlighted. Explain why this happens, and describe how you would redesign the child so the parent can switch highlighting on or off naturally.
Show answer
The attribute value "false" is a string, and the child receives that string. Any non-empty string is truthy, so the template treats the property as true. The root problem is the true default, which forces parents to pass an explicit false value. Redesign the child with a property that defaults to false, for example @api plain = false;, or flip the meaning so the highlight is opt-in: @api highlighted = false;. Then a parent writes highlighted to turn it on, omits the attribute to leave it off, and uses highlighted={isImportant} for dynamic cases.
Exercise
A parent wants to expand an c-info-panel only when its items array is empty. Why does expanded={items.length === 0} fail, and what should the parent do instead?
Show answer
LWC template expressions accept only a property or a getter path, not operators or comparisons. The parent should define a getter, such as get hasNoItems() { return this.items.length === 0; }, and bind with expanded={hasNoItems}. The getter returns a real Boolean, so the child receives true or false.