So there is a Parent Aura component and a Child LWC component. I have to take a user input in the Parent Aura component, and now in the child LWC component, I have to show that input. I am pretty new in salesforce so please someone explain me with a code on how I would do it.
-
2"Do my work for me" questions don't do well here. You should show us what you've tried and make your question narrow and specific. If you're new to any part of Salesforce, then trailhead.salesforce.com is going to be a better place to get started.– Derek FCommented Jul 13, 2022 at 11:43
2 Answers
To dynamically get the user input, capture the user entered data in javascript controller on onchange event-
To capture the entered value dynamically in javascript use the below code snippet-
event.getSource().get("v.value");
Further to pass the user entered value from Aura to LWC, Create an @Api javascript property in lwc and set it's corresponding value from Parent Aura component. You can refer the documentation for information.
Aura HTML Component :
<aura:component implements="flexipage:availableForRecordHome" access="global">
<aura:attribute name="parentAttribute" type="String"/>
<lightning:input name="Please Enter Some value" label="Enter some text" onchange="{!c.captureUserInput}"/>
<!-- The below component is a LWC element -->
<c:testLwc attributeFromParent="{!v.parentAttribute}"/>
</aura:component>
Aura JS Controller :
({
captureUserInput : function(component, event, helper) {
let userInput = event.getSource().get("v.value");
component.set("v.parentAttribute", userInput);
}
})
LWC HTML Component(Name ===>"testLwc"):
<template>
This is LWC : {attributeFromParent}
</template>
LWC JS Controller :
import { LightningElement,api } from 'lwc';
export default class TestLwc extends LightningElement {
@api attributeFromParent;
}
-
Downvoted because this is a code dump. The explanation is as important as the solution itself.– Derek FCommented Jul 13, 2022 at 11:41
-
-
@SayanBan .. If you found this helpful, please accept the answer. Commented Jul 13, 2022 at 13:21
-
You need to create an @api attribute on the child LWC and pass the value from the parent AURA after taking the user input.
check the following link for code example of getting user input in aura: Easiest way to get a value from a lightning:inputField
check the following link for code example of passing value from aura to lwc: https://salesforcediaries.com/2019/09/15/pass-value-to-lwc-from-parent-aura-component/
-
Yes, I have seen this website code. But this is passing Static value. I am talking about passing dynamic, user input values. I can't find that anywhere. Commented Jul 13, 2022 at 5:55
-
Are you not able to pass the dynamic value from the above method ? Commented Jul 13, 2022 at 6:00
-
In that website, they have just created a variable called card title in child, and in the parent, they just gave card title = "A static value". That I know how to do. I am asking in this same scenario, what if I want to take input from user instead of mentioning this "A static value". Commented Jul 13, 2022 at 6:10
-
In the question you have mentioned you have already taken the input from the user. Just pass that value on the @api attribute of the LWC. Commented Jul 13, 2022 at 6:14
-