The JavaScript file for a Lightning web component must include at least this code, where MyComponent is the name you assign your component class.
import { LightningElement } from 'lwc';export default class MyComponent extends LightningElement {}
The export statement defines a class that extends the LightningElement class. As a best practice, the name of the class usually matches the file name of the JavaScript class, but it’s not a requirement.
Lightning Web Components uses modules (built-in modules were introduced in ECMAScript 6) to bundle core functionality and make it accessible to the JavaScript in your component file. The core module for Lightning web components is lwc.
Begin the module with the import statement and specify the functionality of the module that your component uses.
The import statement indicates the JavaScript uses the LightningElement functionality from the lwc module.
// import module elementsimport { LightningElement} from 'lwc';// declare class to expose the componentexport default class App extends LightningElement {ready = false;// use lifecycle hookconnectedCallback() {setTimeout(() => {this.ready = true;}, 3000);}}
LightningElement is the base class for Lightning web components, which allows us to use connectedCallback().
The connectedCallback() method is one of our lifecycle hooks. You’ll learn more about lifecycle hooks in the next section. For now, know that the method is triggered when a component is inserted in the document object model (DOM). In this case, it starts the timer.
Lifecycle Hooks
Lightning Web Components provides methods that allow you to “hook” your code up to critical events in a component’s lifecycle. These events include when a component is:
- Created
- Added to the DOM
- Rendered in the browser
- Encountering errors
- Removed from the DOM
Respond to any of these lifecycle events using callback methods. For example, the connectedCallback() is invoked when a component is inserted into the DOM. The disconnectedCallback() is invoked when a component is removed from the DOM.
In the JavaScript file we used to test our conditional rendering, we used the connectedCallback() method to automatically execute code when the component is inserted into the DOM. The code waits 3 seconds, then sets ready to true.
import { LightningElement } from 'lwc';export default class App extends LightningElement {ready = false;connectedCallback() {setTimeout(() => {this.ready = true;}, 3000);}}
Also, notice that we used the this keyword. Keyword usage should be familiar if you’ve written JavaScript,
and behaves just like it does in other environments. The this keyword in JavaScript refers to the top level of the current context. Here, the context is this class. The connectedCallback() method assigns the value for the top level ready variable. It’s a great example of how Lightning Web Components lets you bring JavaScript features into your development. You can find a link to good information about this in the Resources section.We’re moving fast and you’ve been able to try out some things. In the next unit, we take a step back and talk more about the environment where the components live.DecoratorsDecorators are often used in JavaScript to modify the behavior of a property or function.To use a decorator, import it from the lwc module and place it before the property or function.import { LightningElement, api } from 'lwc';export default class MyComponent extends LightningElement{@api message;}You can import multiple decorators, but a single property or function can have only one decorator. For example, a property can’t have @api and @wire decorators.Examples of Lightning Web Components decorators include:@api: Marks a field as public. Public properties define the API for a component. An owner component that uses the component in its HTML markup can access the component’s public properties.All public properties are reactive, which means that the framework observes the property for changes. When the property’s value changes, the framework reacts and rerenders the component.All fields are reactive. If the value of a field changes and the field is used in a template—or in the getter of a property used in a template—the framework rerenders the component. You don’t need to decorate the field with @track.@track: Tells the framework to observe changes to the properties of an object or to the elements of an array. If a change occurs, the framework rerenders the component. Use @track only if a field contains an object or an array and if you want the framework to observe changes to the properties of the object or to the elements of the array. If you want to change the value of the whole property, you don’t need to use @track.
No comments:
Post a Comment