Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
How can I check if an unnamed slot is empty with StencilJS?

This is how I would use my component

<stencil-button icon="done">
   Hello world
</stencil-button>

I'd like to avoid to pass a slot named html fragment to my component every time

<stencil-button icon="done">
   <span slot="text">Hello world</span>
</stencil-button>

In render() function, I'm used to hide an element if I dont's pass named slots to it:

render () {
    return (
      <Host>
        { this.icon && <mds-icon name={this.icon} /> }
        { this.hasText && <mds-text typography={...}><slot name="text"/></mds-text> }
      </Host>
    )
  }

With named slots, I'm used to do this:

componentWillLoad ():void {
  this.hasText = this.hostElement.querySelector('[slot="text"]') !== null
}

But with unnamed slots i cannot find a way to do it, I've tried some ways without success:

render () {
    return (
      <Host>
        { this.icon && <mds-icon name={this.icon} /> }
        { this.hasText && <mds-text typography={...}><slot/></mds-text> }
      </Host>
    )
  }

I thought it worked by checking if I dont's pass innerHTML to it:

componentWillLoad ():void {
  this.hasText = this.hostElement.innerHTML !== ''
}

This won't work because innerHTML changes in componentDidRender().

So, how can I do that without be forced to pass an element?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I dunno why it wasn't working, but this is working form me:

componentWillLoad ():void {
  this.hasText = this.hostElement.innerHTML !== ''
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!