Concepts or coding lessons of Salesforce that you can implement easily

Lightning Web Component Interview Questions - Part 3

 Below are few questions which are asked me during my latest interviews : 

 22. I want to pass data from HTML file of Parent to Child in LWC?

Ans: WE can use slots to pass data from HTML file of Parent to Child.


23. What are the slots in LWC, explain with code ?

Ans: Slots allows component to interact, display and receive dynamic content from parent components.

Parent.html

<template>

  <c-child-slot>

    <p> i am unnamed slot</p>

    <p slot="first"> i am unnamed slot new !</p>

  </c-child-slot>

</template>


ChildSlot.html

<template>

  <slot></slot>

  <slot name="first" onslotchange={handlechange}></slot>

</template>


Child.js

import { LightningElement } from 'lwc';

export default class ChildSlot extends LightningElement {

  handlechange(){

    console.log('in handlechange');

  }

}

OUTPUT: 


24. What are the benefits slots in LWC?

Ans: 

  • Slots enable LWC components to interact dynamic content provided by Parent LWC. 
  • This helps in building of complex UI with multiple components.
  • Slots provides reusability, as its allows Child LWC to work with different Parent LWC.
  • Slots simplifying the overall application architecture.

25. What are the Promises in LWC?

Ans: In LWC, when we have to call methods Asynchronously, we can use Promises. Promises handles asynchronous operations and provide bettor error handling than events and callback.

Syntax: 

var objPromise = new Promise(Function(resolve, reject)){

    // your async code.

});

Promise takes one argument which is a callback function. Callback function have 2 parameters, resolve and reject. If everything works well then call resolve else reject.

Promises have 3 steps: Pending, Fulfilled, Rejected.


26. When to use @wire call?

Ans:

  • We can use wire when we have to cached the Result. 
  • @wire adaptor are great to read the apex data.
  • DML are not supported in @wire.

27. When to use Imperative call?

Ans:
  • When we have to do DML operations on database.
  • Data cannot be cached.

28. Why we set super() in LWC constructor?

Ans: Constructor must have super() as first statement. This establishes the correct prototype chain and value for "this" keyword.


29. What is the use of this keyword in LWC?

Ans:  this keyword is used to gain access of property or method of any LWC component. this referred to calling context of current piece of code.


30. Why we use @track decorator?

Ans:  @track decorator is use to make variable Private and track Private reactive properties in LWC.


31. Why we use @api decorator?

Ans:  @api decorator is use to make variable Public and track Public reactive properties in LWC.


Subscribe blog for More updates !!! 



Read More: Lightning Web Component Interview Questions - Part 1
                    


Latest Salesforce Interview Questions and Answers: