Sub topic 6
Boolean properties and why they should default to false
This chapter assumes you can build a simple Lightning web component, use {property} expressions in a template, and set a child's @api property from a parent. It covers one narrow topic: how boolean public properties behave in markup, and why they should start as false.
How HTML treats boolean attributes
In plain HTML, a boolean attribute is true when it is present and false when it is absent. The value does not matter. <input disabled> and <input disabled="false"> both produce a disabled input.
LWC follows the same model for public properties. If the parent writes the attribute name with no value, the child receives true. If the parent leaves the attribute out, the child keeps whatever default it declared.
A child with a boolean property
This child shows a highlighted style when urgent is set.
// taskCard.js (child)
import { LightningElement, api } from 'lwc';
export default class TaskCard extends LightningElement {
@api heading;
@api urgent = false;
get cardClass() {
return this.urgent ? 'card urgent' : 'card';
}
}<!-- taskCard.html (child) -->
<template>
<article class={cardClass}>
<h3>{heading}</h3>
</article>
</template>The parent turns the flag on by writing the attribute with no value:
<!-- taskBoard.html (parent) -->
<template>
<c-task-card heading="Fix login bug" urgent></c-task-card>
<c-task-card heading="Update the wiki"></c-task-card>
</template>The first card receives urgent = true. The second never mentions the attribute, so it keeps the default false.
Why the default must be false
The only way a parent can set a boolean attribute to true in markup is to write it. There is no markup form that sets it back to false once the default is true. Consider a property that defaults to true:
@api visible = true; // problemA parent that wants visible to be false has two options. It can bind a JavaScript value, as in visible={isShown}. Or it can write visible="false", which does not work. In LWC, the string "false" is a non-empty string, so it is truthy. The child sees a truthy value and stays visible.
With a default of false, the common cases are straightforward. Writing the attribute means yes, and omitting it means no. This matches how built-in HTML attributes such as disabled, hidden and checked behave, so a reader of the parent's markup can predict the result.
Salesforce's guidance for LWC follows this rule: a boolean public property should be initialized to false, or left undefined.
<!-- Looks like "not urgent", but the child receives the string "false" -->
<c-task-card heading="Fix login bug" urgent="false"></c-task-card>Because "false" is a non-empty string, it is truthy in JavaScript. Even with the safe default, this markup passes a truthy value, so cardClass returns 'card urgent'. Never write ="false" for a boolean property.
Setting a boolean from the parent's JavaScript
When the flag depends on data, bind it with braces. The bound value is a real boolean, so both true and false work.
<!-- taskBoard.html (parent) -->
<template>
<c-task-card heading="Fix login bug" urgent={isUrgent}></c-task-card>
</template>// taskBoard.js (parent)
import { LightningElement } from 'lwc';
export default class TaskBoard extends LightningElement {
isUrgent = false;
}If isUrgent later changes to true, the child re-renders with the new value.
Summary of the rules
| Parent markup | Value the child receives |
|---|---|
| `urgent` | `true` |
| `urgent={isUrgent}` | the boolean in `isUrgent` |
| `urgent="false"` | the string `"false"`, which is truthy |
| attribute omitted | the child's default |
Exercise
A child declares @api highlighted = true;. A parent writes <c-task-card highlighted="false"></c-task-card>. Is the card highlighted, and how would you redesign the property so the parent can control it from markup?
Show answer
The card is highlighted, because "false" is a non-empty string and therefore truthy. Redesign the property to default to false (for example @api highlighted = false;, or rename it to something like plain if the common case is highlighted). The parent then writes highlighted to turn it on, omits it to leave it off, or binds highlighted={isHighlighted} when the value comes from JavaScript.