Use properties or slot for pass data to child element












1















I've a question about what it is the best way to pass some value from a father component to a child component and show the value, because I've tried to pass value with properties and slot and both ways work.
Properties: I've a movement list and use repeat from lit-html (and method html to render) in the father component in order to create n child component giving the values in them properties.



//father component
render(){
return html`
${repeat(movements, movement => movement.id, (movement, index)=> html`
<movement
.id=${movement.id} .description=${movement.description} .amount=${movement.amount} .balance=${movement.balance}>
</movement>
<hr>
`)}
`
}

//child component
render(){
return html`
<dt>${this.id}</dt>
<dl>${this.description}</dl>
<dl>${this.amount}</dl>
<dl>${this.balance}</dl>
`;
}


Slot: I've a movement list and use repeat from lit-html (and method html to render) in the father component in order to create n child component giving the values in slot which were declared in the child component



//child component
render(){
return html`
<dd>
<slot name="id"></slot>
<slot name="description"></slot>
<slot name="amount"></slot>
<slot name="balance"></slot>
</dd>
`;
}

//father component
render(){
return html`
${repeat(movements, movement=>movement.id, (movement, index)=>html`
<movement>
<dt slot="id"> ${movement.id} </dt>
<dl slot="description"> ${movement.description} </dl>
<dl slot="amount"> ${movement.amount} </dl>
<dl slot="balance"> ${movement.balance} </dl>
</movement>
`)}
`;
}


Which it is the best way? When do I have to use one and the other?










share|improve this question























  • Why use lit-html in the first place? React easily supports templates and repetition. So the answer is: neither, which will also get rid of this, sorry, really ugly syntax.

    – Chris G
    Nov 15 '18 at 10:50













  • Plus, I think you're using those tags wrong; dl is the outermost one, containing a list of dt,dd pairs: developer.mozilla.org/en-US/docs/Web/HTML/Element/dl

    – Chris G
    Nov 15 '18 at 10:59













  • Yes, the internal tag are wrong but for the example it isn't important. I don't use React, I use Lit-Element

    – Ismael Rodriguez
    Nov 15 '18 at 11:45











  • Oh, sorry, please ignore me :) I saw render() and "component" and my went automatically went to React :)

    – Chris G
    Nov 15 '18 at 11:55
















1















I've a question about what it is the best way to pass some value from a father component to a child component and show the value, because I've tried to pass value with properties and slot and both ways work.
Properties: I've a movement list and use repeat from lit-html (and method html to render) in the father component in order to create n child component giving the values in them properties.



//father component
render(){
return html`
${repeat(movements, movement => movement.id, (movement, index)=> html`
<movement
.id=${movement.id} .description=${movement.description} .amount=${movement.amount} .balance=${movement.balance}>
</movement>
<hr>
`)}
`
}

//child component
render(){
return html`
<dt>${this.id}</dt>
<dl>${this.description}</dl>
<dl>${this.amount}</dl>
<dl>${this.balance}</dl>
`;
}


Slot: I've a movement list and use repeat from lit-html (and method html to render) in the father component in order to create n child component giving the values in slot which were declared in the child component



//child component
render(){
return html`
<dd>
<slot name="id"></slot>
<slot name="description"></slot>
<slot name="amount"></slot>
<slot name="balance"></slot>
</dd>
`;
}

//father component
render(){
return html`
${repeat(movements, movement=>movement.id, (movement, index)=>html`
<movement>
<dt slot="id"> ${movement.id} </dt>
<dl slot="description"> ${movement.description} </dl>
<dl slot="amount"> ${movement.amount} </dl>
<dl slot="balance"> ${movement.balance} </dl>
</movement>
`)}
`;
}


Which it is the best way? When do I have to use one and the other?










share|improve this question























  • Why use lit-html in the first place? React easily supports templates and repetition. So the answer is: neither, which will also get rid of this, sorry, really ugly syntax.

    – Chris G
    Nov 15 '18 at 10:50













  • Plus, I think you're using those tags wrong; dl is the outermost one, containing a list of dt,dd pairs: developer.mozilla.org/en-US/docs/Web/HTML/Element/dl

    – Chris G
    Nov 15 '18 at 10:59













  • Yes, the internal tag are wrong but for the example it isn't important. I don't use React, I use Lit-Element

    – Ismael Rodriguez
    Nov 15 '18 at 11:45











  • Oh, sorry, please ignore me :) I saw render() and "component" and my went automatically went to React :)

    – Chris G
    Nov 15 '18 at 11:55














1












1








1








I've a question about what it is the best way to pass some value from a father component to a child component and show the value, because I've tried to pass value with properties and slot and both ways work.
Properties: I've a movement list and use repeat from lit-html (and method html to render) in the father component in order to create n child component giving the values in them properties.



//father component
render(){
return html`
${repeat(movements, movement => movement.id, (movement, index)=> html`
<movement
.id=${movement.id} .description=${movement.description} .amount=${movement.amount} .balance=${movement.balance}>
</movement>
<hr>
`)}
`
}

//child component
render(){
return html`
<dt>${this.id}</dt>
<dl>${this.description}</dl>
<dl>${this.amount}</dl>
<dl>${this.balance}</dl>
`;
}


Slot: I've a movement list and use repeat from lit-html (and method html to render) in the father component in order to create n child component giving the values in slot which were declared in the child component



//child component
render(){
return html`
<dd>
<slot name="id"></slot>
<slot name="description"></slot>
<slot name="amount"></slot>
<slot name="balance"></slot>
</dd>
`;
}

//father component
render(){
return html`
${repeat(movements, movement=>movement.id, (movement, index)=>html`
<movement>
<dt slot="id"> ${movement.id} </dt>
<dl slot="description"> ${movement.description} </dl>
<dl slot="amount"> ${movement.amount} </dl>
<dl slot="balance"> ${movement.balance} </dl>
</movement>
`)}
`;
}


Which it is the best way? When do I have to use one and the other?










share|improve this question














I've a question about what it is the best way to pass some value from a father component to a child component and show the value, because I've tried to pass value with properties and slot and both ways work.
Properties: I've a movement list and use repeat from lit-html (and method html to render) in the father component in order to create n child component giving the values in them properties.



//father component
render(){
return html`
${repeat(movements, movement => movement.id, (movement, index)=> html`
<movement
.id=${movement.id} .description=${movement.description} .amount=${movement.amount} .balance=${movement.balance}>
</movement>
<hr>
`)}
`
}

//child component
render(){
return html`
<dt>${this.id}</dt>
<dl>${this.description}</dl>
<dl>${this.amount}</dl>
<dl>${this.balance}</dl>
`;
}


Slot: I've a movement list and use repeat from lit-html (and method html to render) in the father component in order to create n child component giving the values in slot which were declared in the child component



//child component
render(){
return html`
<dd>
<slot name="id"></slot>
<slot name="description"></slot>
<slot name="amount"></slot>
<slot name="balance"></slot>
</dd>
`;
}

//father component
render(){
return html`
${repeat(movements, movement=>movement.id, (movement, index)=>html`
<movement>
<dt slot="id"> ${movement.id} </dt>
<dl slot="description"> ${movement.description} </dl>
<dl slot="amount"> ${movement.amount} </dl>
<dl slot="balance"> ${movement.balance} </dl>
</movement>
`)}
`;
}


Which it is the best way? When do I have to use one and the other?







javascript polymer web-component lit-element lit-html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 10:47









Ismael RodriguezIsmael Rodriguez

92




92













  • Why use lit-html in the first place? React easily supports templates and repetition. So the answer is: neither, which will also get rid of this, sorry, really ugly syntax.

    – Chris G
    Nov 15 '18 at 10:50













  • Plus, I think you're using those tags wrong; dl is the outermost one, containing a list of dt,dd pairs: developer.mozilla.org/en-US/docs/Web/HTML/Element/dl

    – Chris G
    Nov 15 '18 at 10:59













  • Yes, the internal tag are wrong but for the example it isn't important. I don't use React, I use Lit-Element

    – Ismael Rodriguez
    Nov 15 '18 at 11:45











  • Oh, sorry, please ignore me :) I saw render() and "component" and my went automatically went to React :)

    – Chris G
    Nov 15 '18 at 11:55



















  • Why use lit-html in the first place? React easily supports templates and repetition. So the answer is: neither, which will also get rid of this, sorry, really ugly syntax.

    – Chris G
    Nov 15 '18 at 10:50













  • Plus, I think you're using those tags wrong; dl is the outermost one, containing a list of dt,dd pairs: developer.mozilla.org/en-US/docs/Web/HTML/Element/dl

    – Chris G
    Nov 15 '18 at 10:59













  • Yes, the internal tag are wrong but for the example it isn't important. I don't use React, I use Lit-Element

    – Ismael Rodriguez
    Nov 15 '18 at 11:45











  • Oh, sorry, please ignore me :) I saw render() and "component" and my went automatically went to React :)

    – Chris G
    Nov 15 '18 at 11:55

















Why use lit-html in the first place? React easily supports templates and repetition. So the answer is: neither, which will also get rid of this, sorry, really ugly syntax.

– Chris G
Nov 15 '18 at 10:50







Why use lit-html in the first place? React easily supports templates and repetition. So the answer is: neither, which will also get rid of this, sorry, really ugly syntax.

– Chris G
Nov 15 '18 at 10:50















Plus, I think you're using those tags wrong; dl is the outermost one, containing a list of dt,dd pairs: developer.mozilla.org/en-US/docs/Web/HTML/Element/dl

– Chris G
Nov 15 '18 at 10:59







Plus, I think you're using those tags wrong; dl is the outermost one, containing a list of dt,dd pairs: developer.mozilla.org/en-US/docs/Web/HTML/Element/dl

– Chris G
Nov 15 '18 at 10:59















Yes, the internal tag are wrong but for the example it isn't important. I don't use React, I use Lit-Element

– Ismael Rodriguez
Nov 15 '18 at 11:45





Yes, the internal tag are wrong but for the example it isn't important. I don't use React, I use Lit-Element

– Ismael Rodriguez
Nov 15 '18 at 11:45













Oh, sorry, please ignore me :) I saw render() and "component" and my went automatically went to React :)

– Chris G
Nov 15 '18 at 11:55





Oh, sorry, please ignore me :) I saw render() and "component" and my went automatically went to React :)

– Chris G
Nov 15 '18 at 11:55












2 Answers
2






active

oldest

votes


















1














Here is an example to pass of object's properties with using LitElement:



DEMO



import { LitElement, html } from '@polymer/lit-element'; 

class MyElement extends LitElement {

static get properties() { return {
movements: {
type:Object
}
}
}

constructor(){
super();
// Initialize property here.
this.movements = [{id:"123", amount:40000, description:"Bu yuzyilin en buyuk lideri Atatürk tür", balance:20000},{id:"123", amount:40000, description:"Tosun was here ! :) ", balance:20000},{id:"123", amount:40000, description:"Ne Mutlu Diyene, Buraarda ırkçı olmayahh :)) ", balance:20000}];
}

//father component
render(){
return html`
${this.movements.map(movement => html`<movement-list .id=${movement.id} .description=${movement.description} .amount=${movement.amount} .balance=${movement.balance}></movement-list>`)}

`;
}
}

class MovementList extends LitElement {

static get properties() { return {
id: {type:String},
description: {type:String},
amount: {type:Number},
balance: {type:Number}
}
}
//child component
render(){
return html`
<dt>${this.id}</dt>
<dl>${this.description}</dl>
<dl>${this.amount}</dl>
<dl>${this.balance}</dl>

`;
}

}
customElements.define('my-element', MyElement);
customElements.define('movement-list', MovementList);





share|improve this answer

































    1














    If you want pass any property, value or similar you must to use a Polymer property if the way is from parent to child (with a dispatch event if it is from child to parent).



    The use of a <slot> is when you have created a space where other dev wants add more content.
    Polymer's guide says:




    To allow children to render, you can add a element to your shadow tree. Think of the as a placeholder showing where child nodes will render.




    So, if you want to pass a value from parent to child I would use a property.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53317709%2fuse-properties-or-slot-for-pass-data-to-child-element%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









      1














      Here is an example to pass of object's properties with using LitElement:



      DEMO



      import { LitElement, html } from '@polymer/lit-element'; 

      class MyElement extends LitElement {

      static get properties() { return {
      movements: {
      type:Object
      }
      }
      }

      constructor(){
      super();
      // Initialize property here.
      this.movements = [{id:"123", amount:40000, description:"Bu yuzyilin en buyuk lideri Atatürk tür", balance:20000},{id:"123", amount:40000, description:"Tosun was here ! :) ", balance:20000},{id:"123", amount:40000, description:"Ne Mutlu Diyene, Buraarda ırkçı olmayahh :)) ", balance:20000}];
      }

      //father component
      render(){
      return html`
      ${this.movements.map(movement => html`<movement-list .id=${movement.id} .description=${movement.description} .amount=${movement.amount} .balance=${movement.balance}></movement-list>`)}

      `;
      }
      }

      class MovementList extends LitElement {

      static get properties() { return {
      id: {type:String},
      description: {type:String},
      amount: {type:Number},
      balance: {type:Number}
      }
      }
      //child component
      render(){
      return html`
      <dt>${this.id}</dt>
      <dl>${this.description}</dl>
      <dl>${this.amount}</dl>
      <dl>${this.balance}</dl>

      `;
      }

      }
      customElements.define('my-element', MyElement);
      customElements.define('movement-list', MovementList);





      share|improve this answer






























        1














        Here is an example to pass of object's properties with using LitElement:



        DEMO



        import { LitElement, html } from '@polymer/lit-element'; 

        class MyElement extends LitElement {

        static get properties() { return {
        movements: {
        type:Object
        }
        }
        }

        constructor(){
        super();
        // Initialize property here.
        this.movements = [{id:"123", amount:40000, description:"Bu yuzyilin en buyuk lideri Atatürk tür", balance:20000},{id:"123", amount:40000, description:"Tosun was here ! :) ", balance:20000},{id:"123", amount:40000, description:"Ne Mutlu Diyene, Buraarda ırkçı olmayahh :)) ", balance:20000}];
        }

        //father component
        render(){
        return html`
        ${this.movements.map(movement => html`<movement-list .id=${movement.id} .description=${movement.description} .amount=${movement.amount} .balance=${movement.balance}></movement-list>`)}

        `;
        }
        }

        class MovementList extends LitElement {

        static get properties() { return {
        id: {type:String},
        description: {type:String},
        amount: {type:Number},
        balance: {type:Number}
        }
        }
        //child component
        render(){
        return html`
        <dt>${this.id}</dt>
        <dl>${this.description}</dl>
        <dl>${this.amount}</dl>
        <dl>${this.balance}</dl>

        `;
        }

        }
        customElements.define('my-element', MyElement);
        customElements.define('movement-list', MovementList);





        share|improve this answer




























          1












          1








          1







          Here is an example to pass of object's properties with using LitElement:



          DEMO



          import { LitElement, html } from '@polymer/lit-element'; 

          class MyElement extends LitElement {

          static get properties() { return {
          movements: {
          type:Object
          }
          }
          }

          constructor(){
          super();
          // Initialize property here.
          this.movements = [{id:"123", amount:40000, description:"Bu yuzyilin en buyuk lideri Atatürk tür", balance:20000},{id:"123", amount:40000, description:"Tosun was here ! :) ", balance:20000},{id:"123", amount:40000, description:"Ne Mutlu Diyene, Buraarda ırkçı olmayahh :)) ", balance:20000}];
          }

          //father component
          render(){
          return html`
          ${this.movements.map(movement => html`<movement-list .id=${movement.id} .description=${movement.description} .amount=${movement.amount} .balance=${movement.balance}></movement-list>`)}

          `;
          }
          }

          class MovementList extends LitElement {

          static get properties() { return {
          id: {type:String},
          description: {type:String},
          amount: {type:Number},
          balance: {type:Number}
          }
          }
          //child component
          render(){
          return html`
          <dt>${this.id}</dt>
          <dl>${this.description}</dl>
          <dl>${this.amount}</dl>
          <dl>${this.balance}</dl>

          `;
          }

          }
          customElements.define('my-element', MyElement);
          customElements.define('movement-list', MovementList);





          share|improve this answer















          Here is an example to pass of object's properties with using LitElement:



          DEMO



          import { LitElement, html } from '@polymer/lit-element'; 

          class MyElement extends LitElement {

          static get properties() { return {
          movements: {
          type:Object
          }
          }
          }

          constructor(){
          super();
          // Initialize property here.
          this.movements = [{id:"123", amount:40000, description:"Bu yuzyilin en buyuk lideri Atatürk tür", balance:20000},{id:"123", amount:40000, description:"Tosun was here ! :) ", balance:20000},{id:"123", amount:40000, description:"Ne Mutlu Diyene, Buraarda ırkçı olmayahh :)) ", balance:20000}];
          }

          //father component
          render(){
          return html`
          ${this.movements.map(movement => html`<movement-list .id=${movement.id} .description=${movement.description} .amount=${movement.amount} .balance=${movement.balance}></movement-list>`)}

          `;
          }
          }

          class MovementList extends LitElement {

          static get properties() { return {
          id: {type:String},
          description: {type:String},
          amount: {type:Number},
          balance: {type:Number}
          }
          }
          //child component
          render(){
          return html`
          <dt>${this.id}</dt>
          <dl>${this.description}</dl>
          <dl>${this.amount}</dl>
          <dl>${this.balance}</dl>

          `;
          }

          }
          customElements.define('my-element', MyElement);
          customElements.define('movement-list', MovementList);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 17:47

























          answered Nov 15 '18 at 17:42









          HakanCHakanC

          2,1843813




          2,1843813

























              1














              If you want pass any property, value or similar you must to use a Polymer property if the way is from parent to child (with a dispatch event if it is from child to parent).



              The use of a <slot> is when you have created a space where other dev wants add more content.
              Polymer's guide says:




              To allow children to render, you can add a element to your shadow tree. Think of the as a placeholder showing where child nodes will render.




              So, if you want to pass a value from parent to child I would use a property.






              share|improve this answer




























                1














                If you want pass any property, value or similar you must to use a Polymer property if the way is from parent to child (with a dispatch event if it is from child to parent).



                The use of a <slot> is when you have created a space where other dev wants add more content.
                Polymer's guide says:




                To allow children to render, you can add a element to your shadow tree. Think of the as a placeholder showing where child nodes will render.




                So, if you want to pass a value from parent to child I would use a property.






                share|improve this answer


























                  1












                  1








                  1







                  If you want pass any property, value or similar you must to use a Polymer property if the way is from parent to child (with a dispatch event if it is from child to parent).



                  The use of a <slot> is when you have created a space where other dev wants add more content.
                  Polymer's guide says:




                  To allow children to render, you can add a element to your shadow tree. Think of the as a placeholder showing where child nodes will render.




                  So, if you want to pass a value from parent to child I would use a property.






                  share|improve this answer













                  If you want pass any property, value or similar you must to use a Polymer property if the way is from parent to child (with a dispatch event if it is from child to parent).



                  The use of a <slot> is when you have created a space where other dev wants add more content.
                  Polymer's guide says:




                  To allow children to render, you can add a element to your shadow tree. Think of the as a placeholder showing where child nodes will render.




                  So, if you want to pass a value from parent to child I would use a property.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 11 '18 at 18:42









                  AlvaroGlezAlvaroGlez

                  38112




                  38112






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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%2fstackoverflow.com%2fquestions%2f53317709%2fuse-properties-or-slot-for-pass-data-to-child-element%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

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values