Dynamic component creation in Lightning Web Components












4














Is there any way to create and add lightning web components dynamically in the component tree ? I mean the equivalent one in Aura is $A.createComponent().



The usual Web Components can be dynamically created through standard DOM APIs like document.createElement('c-my-cmp') or element.appendChild(element), but as far as I tried in pre-release org it would not work at all.










share|improve this question
























  • You can read my detailed answer here: salesforce.stackexchange.com/questions/244847/…
    – Diego
    2 days ago
















4














Is there any way to create and add lightning web components dynamically in the component tree ? I mean the equivalent one in Aura is $A.createComponent().



The usual Web Components can be dynamically created through standard DOM APIs like document.createElement('c-my-cmp') or element.appendChild(element), but as far as I tried in pre-release org it would not work at all.










share|improve this question
























  • You can read my detailed answer here: salesforce.stackexchange.com/questions/244847/…
    – Diego
    2 days ago














4












4








4







Is there any way to create and add lightning web components dynamically in the component tree ? I mean the equivalent one in Aura is $A.createComponent().



The usual Web Components can be dynamically created through standard DOM APIs like document.createElement('c-my-cmp') or element.appendChild(element), but as far as I tried in pre-release org it would not work at all.










share|improve this question















Is there any way to create and add lightning web components dynamically in the component tree ? I mean the equivalent one in Aura is $A.createComponent().



The usual Web Components can be dynamically created through standard DOM APIs like document.createElement('c-my-cmp') or element.appendChild(element), but as far as I tried in pre-release org it would not work at all.







lightning-web-components






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 17 at 16:07









Mohith Shrivastava

59.8k796138




59.8k796138










asked Dec 17 at 15:21









stomita

1212




1212












  • You can read my detailed answer here: salesforce.stackexchange.com/questions/244847/…
    – Diego
    2 days ago


















  • You can read my detailed answer here: salesforce.stackexchange.com/questions/244847/…
    – Diego
    2 days ago
















You can read my detailed answer here: salesforce.stackexchange.com/questions/244847/…
– Diego
2 days ago




You can read my detailed answer here: salesforce.stackexchange.com/questions/244847/…
– Diego
2 days ago










2 Answers
2






active

oldest

votes


















6














This question has been posted multiple times in the pilot group . The short answer is there is no equivalent for the $A.createComponent() API currently in the LWC .



Currently the team recommends below options



1) Create if branches on the template only load them when a certain condition is meet (yes this is no ideal and not what you are looking for).



2) You can have multiple html templates in your component. You can have a render() method that can switch which template to use based on some conditions.



Looks like option 2 seems more viable approach .



Below is the example shown for this use case



import { LightningElement } from "lwc";

import loadingTemplate from "./loading.html";
import finalTemplate from "./fancyTemplate.html";

export default class HelloWorld extends LightningElement {
@track isLoading = false;

render() {
return this.isLoading ? loadingTemplate : finalTemplate;
}
}


Providing true dynamic creation seems to be technically challenge as per the conversation in the pilot chatter group .






share|improve this answer





















  • The TLDR here is that this is much, much, much more performant.
    – joshbirk
    Dec 17 at 16:54






  • 1




    Neither of the above is helping. I'm currently seeking a possibility to bring a React app to LWC, which is dynamically rendering DOMs via react-dom. When I heard about LWC first, I thought it would become possible as it is based on the standard (custom elements). Additionally I also heard someone is saying "LWC components are 'first-class' DOM elements". If we cannot create components through the DOM API, we should stop the applause.
    – stomita
    Dec 18 at 4:02










  • I think you should present what you need to LWC team and see what's in roadmap .I am sure since it's start there will be plans to provide something along those lines .
    – Mohith Shrivastava
    Dec 18 at 4:05



















1














I don't have access to a preview org so this is 100% based on the playground.



I'm quite interested in this ability as well as it's something I've seen used heavily within lightning components (now aura components).



I played around with it in the playground and there does seem to be decent support for this.



The playground gives you main.js which contains the following code by default:



// This is the main entry point to the playground. By default,
// it simply creates a single lightning web component, and adds
// it to the DOM tree.

import * as Engine from 'lwc';
import App from 'c-app';

const element = Engine.createElement('c-app', { is: App });
document.body.appendChild(element);


Based off this (the last two lines) I was able to:




  1. Create a component, c-example which exports the class Example that does nothing except render a div with some text using an @api field called name

  2. Create a component, c-renderer which has two @api fields, component-name which is the string name of the class (c-example) and component-class which is a reference to the class (Example)

  3. Render an element of the class dynamically using the code from main.js

  4. Alter the Example's name field directly through JavaScript


The point of this was not to create something useful (this is a component that really does nothing when you think about it) but to test the ease with which you can not only render components but render them without knowledge of what component you're rendering. I think this is a key feature for building heavily dynamic components. It also tests the ability to pass data to these components after rendering them which is also key.



The full playground is available here.



The gist of the code is:



example.js



import { LightningElement, api } from 'lwc';

export default class Example extends LightningElement {
@api
name = 'default';
}


example.html



<template>
<div>
This is dynamically rendered content. Its name is {name}
</div>
</template>


renderer.js



import { LightningElement, api, createElement } from 'lwc';

export default class Renderer extends LightningElement {
_componentName;

@api get componentName() {
this._componentName;
}

set componentName(value) {
this._componentName = value;
this.renderContent();
}

_componentClass;

@api get componentClass() {
this._componentClass;
}

set componentClass(value) {
this._componentClass = value;
this.renderContent();
}

renderContent() {
if (!this._componentClass || !this._componentName) return;

const element = createElement(
this._componentName,
{ is: this._componentClass }
);

element.name = 'not default';

document.body.appendChild(element);
}
}


renderer.html



<template>
<div>
This is statically rendered content
</div>
</template>


app.js



import { LightningElement, track } from 'lwc';
import Example from 'c-example';

export default class App extends LightningElement {
// I could not reference Example directly. It had to be in a variable.
componentClass = Example;
}


app.html



<template>
<c-renderer component-name="c-example" component-class={componentClass}></c-renderer>
</template>





share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "459"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f243876%2fdynamic-component-creation-in-lightning-web-components%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    This question has been posted multiple times in the pilot group . The short answer is there is no equivalent for the $A.createComponent() API currently in the LWC .



    Currently the team recommends below options



    1) Create if branches on the template only load them when a certain condition is meet (yes this is no ideal and not what you are looking for).



    2) You can have multiple html templates in your component. You can have a render() method that can switch which template to use based on some conditions.



    Looks like option 2 seems more viable approach .



    Below is the example shown for this use case



    import { LightningElement } from "lwc";

    import loadingTemplate from "./loading.html";
    import finalTemplate from "./fancyTemplate.html";

    export default class HelloWorld extends LightningElement {
    @track isLoading = false;

    render() {
    return this.isLoading ? loadingTemplate : finalTemplate;
    }
    }


    Providing true dynamic creation seems to be technically challenge as per the conversation in the pilot chatter group .






    share|improve this answer





















    • The TLDR here is that this is much, much, much more performant.
      – joshbirk
      Dec 17 at 16:54






    • 1




      Neither of the above is helping. I'm currently seeking a possibility to bring a React app to LWC, which is dynamically rendering DOMs via react-dom. When I heard about LWC first, I thought it would become possible as it is based on the standard (custom elements). Additionally I also heard someone is saying "LWC components are 'first-class' DOM elements". If we cannot create components through the DOM API, we should stop the applause.
      – stomita
      Dec 18 at 4:02










    • I think you should present what you need to LWC team and see what's in roadmap .I am sure since it's start there will be plans to provide something along those lines .
      – Mohith Shrivastava
      Dec 18 at 4:05
















    6














    This question has been posted multiple times in the pilot group . The short answer is there is no equivalent for the $A.createComponent() API currently in the LWC .



    Currently the team recommends below options



    1) Create if branches on the template only load them when a certain condition is meet (yes this is no ideal and not what you are looking for).



    2) You can have multiple html templates in your component. You can have a render() method that can switch which template to use based on some conditions.



    Looks like option 2 seems more viable approach .



    Below is the example shown for this use case



    import { LightningElement } from "lwc";

    import loadingTemplate from "./loading.html";
    import finalTemplate from "./fancyTemplate.html";

    export default class HelloWorld extends LightningElement {
    @track isLoading = false;

    render() {
    return this.isLoading ? loadingTemplate : finalTemplate;
    }
    }


    Providing true dynamic creation seems to be technically challenge as per the conversation in the pilot chatter group .






    share|improve this answer





















    • The TLDR here is that this is much, much, much more performant.
      – joshbirk
      Dec 17 at 16:54






    • 1




      Neither of the above is helping. I'm currently seeking a possibility to bring a React app to LWC, which is dynamically rendering DOMs via react-dom. When I heard about LWC first, I thought it would become possible as it is based on the standard (custom elements). Additionally I also heard someone is saying "LWC components are 'first-class' DOM elements". If we cannot create components through the DOM API, we should stop the applause.
      – stomita
      Dec 18 at 4:02










    • I think you should present what you need to LWC team and see what's in roadmap .I am sure since it's start there will be plans to provide something along those lines .
      – Mohith Shrivastava
      Dec 18 at 4:05














    6












    6








    6






    This question has been posted multiple times in the pilot group . The short answer is there is no equivalent for the $A.createComponent() API currently in the LWC .



    Currently the team recommends below options



    1) Create if branches on the template only load them when a certain condition is meet (yes this is no ideal and not what you are looking for).



    2) You can have multiple html templates in your component. You can have a render() method that can switch which template to use based on some conditions.



    Looks like option 2 seems more viable approach .



    Below is the example shown for this use case



    import { LightningElement } from "lwc";

    import loadingTemplate from "./loading.html";
    import finalTemplate from "./fancyTemplate.html";

    export default class HelloWorld extends LightningElement {
    @track isLoading = false;

    render() {
    return this.isLoading ? loadingTemplate : finalTemplate;
    }
    }


    Providing true dynamic creation seems to be technically challenge as per the conversation in the pilot chatter group .






    share|improve this answer












    This question has been posted multiple times in the pilot group . The short answer is there is no equivalent for the $A.createComponent() API currently in the LWC .



    Currently the team recommends below options



    1) Create if branches on the template only load them when a certain condition is meet (yes this is no ideal and not what you are looking for).



    2) You can have multiple html templates in your component. You can have a render() method that can switch which template to use based on some conditions.



    Looks like option 2 seems more viable approach .



    Below is the example shown for this use case



    import { LightningElement } from "lwc";

    import loadingTemplate from "./loading.html";
    import finalTemplate from "./fancyTemplate.html";

    export default class HelloWorld extends LightningElement {
    @track isLoading = false;

    render() {
    return this.isLoading ? loadingTemplate : finalTemplate;
    }
    }


    Providing true dynamic creation seems to be technically challenge as per the conversation in the pilot chatter group .







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 17 at 16:07









    Mohith Shrivastava

    59.8k796138




    59.8k796138












    • The TLDR here is that this is much, much, much more performant.
      – joshbirk
      Dec 17 at 16:54






    • 1




      Neither of the above is helping. I'm currently seeking a possibility to bring a React app to LWC, which is dynamically rendering DOMs via react-dom. When I heard about LWC first, I thought it would become possible as it is based on the standard (custom elements). Additionally I also heard someone is saying "LWC components are 'first-class' DOM elements". If we cannot create components through the DOM API, we should stop the applause.
      – stomita
      Dec 18 at 4:02










    • I think you should present what you need to LWC team and see what's in roadmap .I am sure since it's start there will be plans to provide something along those lines .
      – Mohith Shrivastava
      Dec 18 at 4:05


















    • The TLDR here is that this is much, much, much more performant.
      – joshbirk
      Dec 17 at 16:54






    • 1




      Neither of the above is helping. I'm currently seeking a possibility to bring a React app to LWC, which is dynamically rendering DOMs via react-dom. When I heard about LWC first, I thought it would become possible as it is based on the standard (custom elements). Additionally I also heard someone is saying "LWC components are 'first-class' DOM elements". If we cannot create components through the DOM API, we should stop the applause.
      – stomita
      Dec 18 at 4:02










    • I think you should present what you need to LWC team and see what's in roadmap .I am sure since it's start there will be plans to provide something along those lines .
      – Mohith Shrivastava
      Dec 18 at 4:05
















    The TLDR here is that this is much, much, much more performant.
    – joshbirk
    Dec 17 at 16:54




    The TLDR here is that this is much, much, much more performant.
    – joshbirk
    Dec 17 at 16:54




    1




    1




    Neither of the above is helping. I'm currently seeking a possibility to bring a React app to LWC, which is dynamically rendering DOMs via react-dom. When I heard about LWC first, I thought it would become possible as it is based on the standard (custom elements). Additionally I also heard someone is saying "LWC components are 'first-class' DOM elements". If we cannot create components through the DOM API, we should stop the applause.
    – stomita
    Dec 18 at 4:02




    Neither of the above is helping. I'm currently seeking a possibility to bring a React app to LWC, which is dynamically rendering DOMs via react-dom. When I heard about LWC first, I thought it would become possible as it is based on the standard (custom elements). Additionally I also heard someone is saying "LWC components are 'first-class' DOM elements". If we cannot create components through the DOM API, we should stop the applause.
    – stomita
    Dec 18 at 4:02












    I think you should present what you need to LWC team and see what's in roadmap .I am sure since it's start there will be plans to provide something along those lines .
    – Mohith Shrivastava
    Dec 18 at 4:05




    I think you should present what you need to LWC team and see what's in roadmap .I am sure since it's start there will be plans to provide something along those lines .
    – Mohith Shrivastava
    Dec 18 at 4:05













    1














    I don't have access to a preview org so this is 100% based on the playground.



    I'm quite interested in this ability as well as it's something I've seen used heavily within lightning components (now aura components).



    I played around with it in the playground and there does seem to be decent support for this.



    The playground gives you main.js which contains the following code by default:



    // This is the main entry point to the playground. By default,
    // it simply creates a single lightning web component, and adds
    // it to the DOM tree.

    import * as Engine from 'lwc';
    import App from 'c-app';

    const element = Engine.createElement('c-app', { is: App });
    document.body.appendChild(element);


    Based off this (the last two lines) I was able to:




    1. Create a component, c-example which exports the class Example that does nothing except render a div with some text using an @api field called name

    2. Create a component, c-renderer which has two @api fields, component-name which is the string name of the class (c-example) and component-class which is a reference to the class (Example)

    3. Render an element of the class dynamically using the code from main.js

    4. Alter the Example's name field directly through JavaScript


    The point of this was not to create something useful (this is a component that really does nothing when you think about it) but to test the ease with which you can not only render components but render them without knowledge of what component you're rendering. I think this is a key feature for building heavily dynamic components. It also tests the ability to pass data to these components after rendering them which is also key.



    The full playground is available here.



    The gist of the code is:



    example.js



    import { LightningElement, api } from 'lwc';

    export default class Example extends LightningElement {
    @api
    name = 'default';
    }


    example.html



    <template>
    <div>
    This is dynamically rendered content. Its name is {name}
    </div>
    </template>


    renderer.js



    import { LightningElement, api, createElement } from 'lwc';

    export default class Renderer extends LightningElement {
    _componentName;

    @api get componentName() {
    this._componentName;
    }

    set componentName(value) {
    this._componentName = value;
    this.renderContent();
    }

    _componentClass;

    @api get componentClass() {
    this._componentClass;
    }

    set componentClass(value) {
    this._componentClass = value;
    this.renderContent();
    }

    renderContent() {
    if (!this._componentClass || !this._componentName) return;

    const element = createElement(
    this._componentName,
    { is: this._componentClass }
    );

    element.name = 'not default';

    document.body.appendChild(element);
    }
    }


    renderer.html



    <template>
    <div>
    This is statically rendered content
    </div>
    </template>


    app.js



    import { LightningElement, track } from 'lwc';
    import Example from 'c-example';

    export default class App extends LightningElement {
    // I could not reference Example directly. It had to be in a variable.
    componentClass = Example;
    }


    app.html



    <template>
    <c-renderer component-name="c-example" component-class={componentClass}></c-renderer>
    </template>





    share|improve this answer


























      1














      I don't have access to a preview org so this is 100% based on the playground.



      I'm quite interested in this ability as well as it's something I've seen used heavily within lightning components (now aura components).



      I played around with it in the playground and there does seem to be decent support for this.



      The playground gives you main.js which contains the following code by default:



      // This is the main entry point to the playground. By default,
      // it simply creates a single lightning web component, and adds
      // it to the DOM tree.

      import * as Engine from 'lwc';
      import App from 'c-app';

      const element = Engine.createElement('c-app', { is: App });
      document.body.appendChild(element);


      Based off this (the last two lines) I was able to:




      1. Create a component, c-example which exports the class Example that does nothing except render a div with some text using an @api field called name

      2. Create a component, c-renderer which has two @api fields, component-name which is the string name of the class (c-example) and component-class which is a reference to the class (Example)

      3. Render an element of the class dynamically using the code from main.js

      4. Alter the Example's name field directly through JavaScript


      The point of this was not to create something useful (this is a component that really does nothing when you think about it) but to test the ease with which you can not only render components but render them without knowledge of what component you're rendering. I think this is a key feature for building heavily dynamic components. It also tests the ability to pass data to these components after rendering them which is also key.



      The full playground is available here.



      The gist of the code is:



      example.js



      import { LightningElement, api } from 'lwc';

      export default class Example extends LightningElement {
      @api
      name = 'default';
      }


      example.html



      <template>
      <div>
      This is dynamically rendered content. Its name is {name}
      </div>
      </template>


      renderer.js



      import { LightningElement, api, createElement } from 'lwc';

      export default class Renderer extends LightningElement {
      _componentName;

      @api get componentName() {
      this._componentName;
      }

      set componentName(value) {
      this._componentName = value;
      this.renderContent();
      }

      _componentClass;

      @api get componentClass() {
      this._componentClass;
      }

      set componentClass(value) {
      this._componentClass = value;
      this.renderContent();
      }

      renderContent() {
      if (!this._componentClass || !this._componentName) return;

      const element = createElement(
      this._componentName,
      { is: this._componentClass }
      );

      element.name = 'not default';

      document.body.appendChild(element);
      }
      }


      renderer.html



      <template>
      <div>
      This is statically rendered content
      </div>
      </template>


      app.js



      import { LightningElement, track } from 'lwc';
      import Example from 'c-example';

      export default class App extends LightningElement {
      // I could not reference Example directly. It had to be in a variable.
      componentClass = Example;
      }


      app.html



      <template>
      <c-renderer component-name="c-example" component-class={componentClass}></c-renderer>
      </template>





      share|improve this answer
























        1












        1








        1






        I don't have access to a preview org so this is 100% based on the playground.



        I'm quite interested in this ability as well as it's something I've seen used heavily within lightning components (now aura components).



        I played around with it in the playground and there does seem to be decent support for this.



        The playground gives you main.js which contains the following code by default:



        // This is the main entry point to the playground. By default,
        // it simply creates a single lightning web component, and adds
        // it to the DOM tree.

        import * as Engine from 'lwc';
        import App from 'c-app';

        const element = Engine.createElement('c-app', { is: App });
        document.body.appendChild(element);


        Based off this (the last two lines) I was able to:




        1. Create a component, c-example which exports the class Example that does nothing except render a div with some text using an @api field called name

        2. Create a component, c-renderer which has two @api fields, component-name which is the string name of the class (c-example) and component-class which is a reference to the class (Example)

        3. Render an element of the class dynamically using the code from main.js

        4. Alter the Example's name field directly through JavaScript


        The point of this was not to create something useful (this is a component that really does nothing when you think about it) but to test the ease with which you can not only render components but render them without knowledge of what component you're rendering. I think this is a key feature for building heavily dynamic components. It also tests the ability to pass data to these components after rendering them which is also key.



        The full playground is available here.



        The gist of the code is:



        example.js



        import { LightningElement, api } from 'lwc';

        export default class Example extends LightningElement {
        @api
        name = 'default';
        }


        example.html



        <template>
        <div>
        This is dynamically rendered content. Its name is {name}
        </div>
        </template>


        renderer.js



        import { LightningElement, api, createElement } from 'lwc';

        export default class Renderer extends LightningElement {
        _componentName;

        @api get componentName() {
        this._componentName;
        }

        set componentName(value) {
        this._componentName = value;
        this.renderContent();
        }

        _componentClass;

        @api get componentClass() {
        this._componentClass;
        }

        set componentClass(value) {
        this._componentClass = value;
        this.renderContent();
        }

        renderContent() {
        if (!this._componentClass || !this._componentName) return;

        const element = createElement(
        this._componentName,
        { is: this._componentClass }
        );

        element.name = 'not default';

        document.body.appendChild(element);
        }
        }


        renderer.html



        <template>
        <div>
        This is statically rendered content
        </div>
        </template>


        app.js



        import { LightningElement, track } from 'lwc';
        import Example from 'c-example';

        export default class App extends LightningElement {
        // I could not reference Example directly. It had to be in a variable.
        componentClass = Example;
        }


        app.html



        <template>
        <c-renderer component-name="c-example" component-class={componentClass}></c-renderer>
        </template>





        share|improve this answer












        I don't have access to a preview org so this is 100% based on the playground.



        I'm quite interested in this ability as well as it's something I've seen used heavily within lightning components (now aura components).



        I played around with it in the playground and there does seem to be decent support for this.



        The playground gives you main.js which contains the following code by default:



        // This is the main entry point to the playground. By default,
        // it simply creates a single lightning web component, and adds
        // it to the DOM tree.

        import * as Engine from 'lwc';
        import App from 'c-app';

        const element = Engine.createElement('c-app', { is: App });
        document.body.appendChild(element);


        Based off this (the last two lines) I was able to:




        1. Create a component, c-example which exports the class Example that does nothing except render a div with some text using an @api field called name

        2. Create a component, c-renderer which has two @api fields, component-name which is the string name of the class (c-example) and component-class which is a reference to the class (Example)

        3. Render an element of the class dynamically using the code from main.js

        4. Alter the Example's name field directly through JavaScript


        The point of this was not to create something useful (this is a component that really does nothing when you think about it) but to test the ease with which you can not only render components but render them without knowledge of what component you're rendering. I think this is a key feature for building heavily dynamic components. It also tests the ability to pass data to these components after rendering them which is also key.



        The full playground is available here.



        The gist of the code is:



        example.js



        import { LightningElement, api } from 'lwc';

        export default class Example extends LightningElement {
        @api
        name = 'default';
        }


        example.html



        <template>
        <div>
        This is dynamically rendered content. Its name is {name}
        </div>
        </template>


        renderer.js



        import { LightningElement, api, createElement } from 'lwc';

        export default class Renderer extends LightningElement {
        _componentName;

        @api get componentName() {
        this._componentName;
        }

        set componentName(value) {
        this._componentName = value;
        this.renderContent();
        }

        _componentClass;

        @api get componentClass() {
        this._componentClass;
        }

        set componentClass(value) {
        this._componentClass = value;
        this.renderContent();
        }

        renderContent() {
        if (!this._componentClass || !this._componentName) return;

        const element = createElement(
        this._componentName,
        { is: this._componentClass }
        );

        element.name = 'not default';

        document.body.appendChild(element);
        }
        }


        renderer.html



        <template>
        <div>
        This is statically rendered content
        </div>
        </template>


        app.js



        import { LightningElement, track } from 'lwc';
        import Example from 'c-example';

        export default class App extends LightningElement {
        // I could not reference Example directly. It had to be in a variable.
        componentClass = Example;
        }


        app.html



        <template>
        <c-renderer component-name="c-example" component-class={componentClass}></c-renderer>
        </template>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 21 at 8:48









        Benjamin Vogler

        111




        111






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Salesforce Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f243876%2fdynamic-component-creation-in-lightning-web-components%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre