Posts

Showing posts from June, 2018

Working with Conditional Attribute Defaults in Lightning

Setting defaults on attributes in lightning can be a useful tool when setting the initial state of components. There is an 'undocumented feature' that allows us to set attribute defaults conditionally, but you may find that this does not work exactly as you would expect. It turns out that setting a conditional default will 'lock' any changes to that attribute, as demonstrated below. In this example we create two attributes 'myString' and 'mySimpleString'. Both attributes have default values but the default on 'myString' is wrapped in a condition. We then trigger an init handler to update the value of both strings to be 'Set Directly' when the component is initialised. The example shows that 'mySimpleString' will update to the new value, but 'myString' does not. For additional complexity, setting a conditional default with an object reference will 'lock' any changes to that attribute, except from th