Boolean attributes on standard HTML Elements are set to true by adding the attribute to the element. The absence of the attribute defaults the attribute to false. Therefore, the default value of an attribute is always false. Lightning web components use the same principle for boolean properties.
Set a Property Statically
Always set the default value for a boolean public property to false. If you set the default value to true instead in bool.js, there’s no way to statically toggle the value to false in markup.
1// bool.js2import{LightningElement, api}from "lwc";34export default class Bool extends LightningElement{5 @api show = false;6}
To set the show property to true, add a show attribute with an empty value to the markup. This version of c-parent displays a value for true for the show property.
The value of show is true, but a show attribute isn’t reflected in the rendered HTML unless you explicitly reflect it by calling setAttribute. See Reflect JavaScript Properties to HTML Attributes.
When a boolean attribute’s value is true, the rendered HTML is <my-component my-attribute>.
Note
Set a Property Dynamically
To toggle the value dynamically, pass down a dynamic computed value from the parent component. For example:
This release is in preview. Features described here don't become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can't guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.