Create LWC Dynamically In LWC Using Lightning Flow On Button Click

Aura Component has the ability to create lightning components dynamically. Lightning Web Component does not had this functionality. So, Developers were using if:true|false directive as an alternative. The drawback with if:true|false directive is:-
1. Markup must exist in lightning web component inside if:true|false directive
2. Every time requirement changes, markup has to be updated results in code change
Now, We can launch flow from LWC directly, we got a better way to dynamically create LWC from Lightning Web Component. lightning-flow base component gives you the ability to launch a lwc placed inside flow with just a button click using startFlow method from another lightning web component.

Create LWC dynamically with Button Click

  • Step 1 – Create a LWC which have a button
  • Step 2 – Create a Flow and place another LWC inside the flow screen
  • Step 3 – Launch the flow on button click, which results in dynamic creation of LWC component

Let’s create the required LWC and Flow, They are:-
1. launchFlowFromButton – LWC
2. LwcCalledInFlow – LWC
3. CallLwcInFlow – Lightning Flow

Its time to jump into the code.


The html file must have lightning-flow component. It will be invoked programmatically on button click.

    <lightning-flow onstatuschange={handleStatusChange}>
    <lightning-button label="Create LWC Dynamically Using Flow" onclick={handleClick}></lightning-button>


handleClick – This method fires on button click and invoke the flow which results in dynamic creation of LWC with Lightning Web Component
inputVariables – This getter will pass the input variable to flow

handleStatusChange – This function invokes when there is a status change in flow

import { LightningElement } from 'lwc';
export default class LaunchFlowFromButton extends LightningElement {
    handleClick() {
        this.template.querySelector('lightning-flow').startFlow('CallLwcInFlow', this.inputVariables);

    get inputVariables() {
        return [];

    handleStatusChange(event) {
        console.log('handleStatusChange', event.detail);


<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="">

Let’s go through the lwc component that needs to be created dynamically on button click via flow.


We have lightning-card which just shows a static message when this component is created dynamically from another lightning web component.

        <h3 slot="title">
            <lightning-icon icon-name="utility:connected_apps" size="small"></lightning-icon>
            This component has been created dynamically from LWC using flow
        <div slot="footer">
            <lightning-badge label="Flow"></lightning-badge>
            <lightning-badge label="LWC"></lightning-badge>
            <lightning-badge label="Awesome"></lightning-badge>
        <p class="slds-p-horizontal_small">Dynamic Ceation of LWC</p>


The JS file does not have any logic.

import { LightningElement } from 'lwc';
export default class LwcCalledInFlow extends LightningElement {}


Make sure to enable the component to be used in lightning flow.

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="">

Now, both the component is done, Let’s create the lightning flow where we will place the lwcCalledInFlow component.

Lightning Screen Flow – CallLwcInFlow

  • Step 1 : Create a new screen flow
  • Step 2 : give the API name to screen, Make sure to uncheck show header and show footer checkbox
Lightning Screen Flow - CallLwcInFlow 
Create dynamic lwc in lwc
dynamically create lwc via flow in lightning web component
  • Step 3 : Add a screen, place the lwc in canvas as below and click done
Create LWC Dynamically Using In LWC Using Lightning Flow
dynamic lwc
  • Step 4 : Save and Activate the flow, The flow will look like as below
create lwc

Demo – Dynamic Creation of LWC using Flow on Button Click in LWC

Add the launchFlowFromButton component on lightning page to test it. You can see on button click, the component has been created dynamically via the flow. Basically, We are launching the flow on button click which results in dynamic creation of LWC.

launch Flow From Button click
dynamic creation of lwc
dynamic creation of lightning web component using button click
create element in lwc

Do you need help?

Are you stuck while working on this requirement? Do you want to get review of your solution? Now, you can book dedicated 1:1 with me on Lightning Web Component completely free.


Schedule a 1:1 Meeting with me

Do not forget to paste your code in comment. It would help others in case they get stuck. Also check out to learn more on LWC.

One comment

Leave a Reply