Search code examples
salesforceapexsalesforce-lightningaura-framework

Aura get boolean from parent to child


I am very new to salesforce. The boolean value in the parent component will be updated by clikcing a button. That is done. Now I want to toggle a class in a child component based on the parent boolean value. So my question is how can I always read the parent boolean value from the child?

parent component

<aura:attribute name="isActive" type="Boolean" default="false"/>

child component

<div class="{!v.siActive ? 'bg-red' : ''}">
    ........
</div>

Solution

  • Have you seen https://developer.salesforce.com/docs/component-library/bundle/aura:valueChange/documentation ?

    Pass the parent attribute to child (in parent have something like <c:myChild isActive="{!v.isActive}"> and then in child define a handler function that will run on value change.

    Actually there are few patterns for parent-child communication, pick what looks easiest/what's closest to what you already have