Using Data – Attributes in Lightning Web Component (LWC)

Sharing is Caring

In Lightning Web Components (LWC), data can be defined and used in components through attributes. Lightning Web Components can be confusing, I created a primer called Introduction to Salesforce Lightning Web Components that you may find helpful.

Attributes are properties defined in the component’s JavaScript file, which can then be accessed and used in the component’s markup.

To define an attribute in an LWC, you can use the @api decorator in the JavaScript file. For example:

import { LightningElement, api } from 'lwc';

export default class MyComponent extends LightningElement {
    @api myAttribute;
}

In this example, the @api decorator is used to define the myAttribute attribute. This attribute can then be accessed in the component’s markup using curly braces, like this:

<template>
    <p>My attribute value is: {myAttribute}</p>
</template>

To set the value of the myAttribute attribute, you can pass it as a property when you include the component in another component or in a page. For example:

<c-my-component myAttribute="Hello, world!"></c-my-component>

In this example, the myAttribute property is set to “Hello, world!”, which will be displayed in the component’s markup when it’s rendered.

You can also use attributes to pass more complex data, such as objects or arrays. This can be a very useful way to pass a lot of data to a component without necessarily needing to pepper the HTMl with tons of Logic.

To do this, you can define an attribute as an object or array type in the JavaScript file, like this:

import { LightningElement, api } from 'lwc';

export default class MyComponent extends LightningElement {
    @api myObject = { name: 'John', age: 30 };
}

The myObject attribute is defined as an object with a name and age property. This attribute can then be accessed and used in the component’s markup just like any other attribute.

<template>
    <p>My object name is: {myObject.name}</p>
    <p>My object age is: {myObject.age}</p>
</template>

When passing an object or array as an attribute, you can also use the JSON.stringify() method to convert the data to a string and then pass it as a string property. Then, you can parse the string back to an object or array in the component’s JavaScript file.

In the parent component, you could pass an object as a string property like this:

<c-my-component my-object={myObjectString}></c-my-component>

In the child component, you can then parse the string property back into an object in the JavaScript file, like this:

import { LightningElement, api } from 'lwc';

export default class MyComponent extends LightningElement {
    @api myObject;

    connectedCallback() {
        this.myObject = JSON.parse(this.myObject);
    }
}

The myObject property is first defined as a string, and then it’s parsed into an object in the connectedCallback() method, which is called after the component is connected to the DOM.

<template>
    <p>My object name is: {myObject.name}</p>
    <p>My object age is: {myObject.age}</p>
</template>

Wrapping it Up

Attributes in LWC provide a powerful way to pass data between components and to define and use properties within a component. By defining attributes with the @api decorator, you can make them accessible to other components and to other parts of your application.

By using complex data types like objects and arrays you can pass these as string properties and then parse them back into their original data types.


Also published on Medium.

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.