19 - @api / parent-child and child -parent communication / @track
In Lightning Web Components (LWC), the @api decorator primarily allows parent components to access and modify properties or call methods defined in the child component. However, it does not work in the reverse direction – a child component cannot directly access the parent’s properties or methods using @api.
Here's a summary of how @api works:
Child Properties Accessible by Parent:
- When you use
@apion a property or method in the child component, it makes that property or method "public" so the parent component can access and set it. - For example, a child component might expose a property with
@api, which allows the parent to pass values to it.
- When you use
Parent Properties are Not Directly Accessible by Child:
- The child component cannot directly access properties or methods of the parent component. LWC maintains a unidirectional data flow from parent to child for properties.
- If a child component needs to "send" data to the parent, it typically does so using events (as we discussed with
dispatchEvent), rather than directly accessing parent properties.
Parent-to-Child Communication:
- The parent can set properties on the child component (thanks to
@api) and can call public methods in the child using@api. - This means that
@apionly enables communication from the parent to the child, allowing the parent to set data or trigger behavior in the child.
- The parent can set properties on the child component (thanks to
Child-to-Parent Communication (via Events):
- For a child to communicate back to the parent, events are used (e.g.,
dispatchEventwith aCustomEvent). - This event-based communication allows the child to send data up to the parent without directly accessing the parent's properties.
- For a child to communicate back to the parent, events are used (e.g.,
Example Summary
Parent can set child properties and call child methods using
@api.- This enables the parent to control certain aspects of the child component.
Child cannot access parent properties or methods directly.
- To send data to the parent, the child dispatches an event.
METHOD 1 : We'll create two components: a parent component (ParentComponent) and a child component (ChildComponent). The parent will pass data to the child, and the child will communicate back to the parent using a custom event.
Step 1: Create the Child Component
Child Component JavaScript:
childComponent.jsDefine a property with
@apito receive data from the parent, and create a method to dispatch an event back to the parent.Child Component HTML:
childComponent.htmlUse the
messageFromParentvariable to display the message received from the parent and create a button that, when clicked, sends a message back.
Step 2: Create the Parent Component
Parent Component JavaScript:
parentComponent.jsIn the parent component, define a variable to store the message to be sent to the child. Also, define a handler function for the event coming from the child.
Parent Component HTML:
parentComponent.htmlUse the child component and pass data to it using
message-from-parent. Set up an event listeneronmessageto handle the custom event from the child.
Explanation
Data Flow from Parent to Child:
- The parent passes a message to the child component using
message-from-parent. - In the child component, this message is displayed using
{messageFromParent}.
- The parent passes a message to the child component using
Event from Child to Parent:
- When the child component's button is clicked, it dispatches a custom event (
sendmessage) with a message for the parent. - The parent component listens to this event using
onsendmessageand handles it withhandleChildMessage, which updatesreceivedMessagewith the message from the child.
In Lightning Web Components (LWC), the @track decorator was used to make a property reactive, so changes to that property would update the component's UI automatically. However, starting in Spring '20, @track became mostly obsolete due to the introduction of reactive proxies, meaning that any change to a field within an object or an array will automatically trigger UI updates without @track. Now, you only need @track in specific scenarios:
Nested Objects and Arrays: When you're working with deeply nested properties, like
myObject.childProperty, you might need@trackif the updates aren't being detected. But in most cases, LWC's reactivity should handle this without@track.Direct Field Reassignments: If you replace the entire object or array (e.g.,
this.myArray = newArray), the reactivity should work without@track.
Here’s a simple example of using @track:
But nowadays, you typically won’t need @track if you're working with a regular assignment or mutation at the top level. For complex state management, you might still find @track useful in exceptional cases, but for most applications, standard reactivity should suffice.
Comments
Post a Comment