stencil icon indicating copy to clipboard operation
stencil copied to clipboard

Props reflect: true not updating DOM attribute

Open liester opened this issue 6 years ago • 17 comments

Stencil version:

 @stencil/[email protected]

I'm submitting a: [X ] bug report [ ] feature request [ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/

Current behavior: I'm assigning a property value of my stencil component via: const myComponent = document.querySelector('my-component'); myComponent.first = 'levi'

My stencil component has: @Prop({reflect:true}) first: string;

The value appears to be assigned correctly in the component and is displayed correctly on the UI. However, the attribute in the DOM is not reflecting the change as I would expect it to. image

Expected behavior: I would expect the attribute 'first' on the DOM to be updated to 'levi'.

Steps to reproduce: Thinking it was something odd I was doing or a dependency I might have added I went back to the stenciljs docs and used the 'Getting Started' steps: https://stenciljs.com/docs/getting-started

  1. npm init stencil
  2. select the "component" starter.
  3. npm install @stencil/core@latest --save-exact
  4. In the generated my-component.tsx file, update: @Prop() first: string; to be @Prop({reflect:true}) first: string;
  5. In index.html, directly under body end tag add:
<script>
  setTimeout(()=>{
    const myComponent = document.querySelector('my-component');
    myComponent.first = 'levi'
  }, 3000)
</script>

Related code:

import { Component, Prop, h } from '@stencil/core';
import { format } from '../../utils/utils';

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.css',
  shadow: true
})
export class MyComponent {
  /**
   * The first name
   */
  @Prop({reflect:true}) first: string;

  /**
   * The middle name
   */
  @Prop() middle: string;

  /**
   * The last name
   */
  @Prop() last: string;

  private getText(): string {
    return format(this.first, this.middle, this.last);
  }

  render() {
    return <div>Hello, World! I'm {this.getText()}</div>;
  }
}
<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0">
  <title>Stencil Component Starter</title>

  <script type="module" src="/build/test-props.esm.js"></script>
  <script nomodule src="/build/test-props.js"></script>

</head>
<body>

  <my-component first="Stencil" last="'Don't call me a framework' JS"></my-component>

</body>
<script>
  setTimeout(()=>{
    const myComponent = document.querySelector('my-component');
    myComponent.first = 'levi'
  }, 3000)
</script>
</html>

Other information: I haven't had much luck googling for this particular issue. The documentation I see on stenciljs' site makes it seem like this is pretty simple and should work. It makes me think I'm not doing something right but I'm not sure what it is. Just for fun I also tried 'reflectToAttr: true' but that didn't seem to help either. Thank you in advance for any help someone can provide :)

liester avatar Dec 07 '19 03:12 liester

I'm still using reflectToAttr because reflect seemed to do nothing for me (and I wasn't sure if it had another purpose)

dgibson666 avatar Dec 09 '19 15:12 dgibson666

Same issue here. @stencil/core v1.8.2 Using "reflect" not "reflectToAttr" since that is how is defined on the documentation.

bsastregx avatar Dec 26 '19 22:12 bsastregx

Is there a way I can give some kind of vote to this issue?

bsastregx avatar Dec 26 '19 22:12 bsastregx

Same issue here. @stencil/core v1.8.3 Using "reflect" not "reflectToAttr" since that is how is defined on the documentation.

danidelgadoz avatar Jan 02 '20 20:01 danidelgadoz

I am not sure but maybe its related to whats written in the Docs about reactive data. So mutating the Object is not triggering the re-render. Instead it needs to change completely, like so myComponent = {...myComponent, first: 'levi'}

stanley85 avatar Jan 07 '20 14:01 stanley85

Same issue here. @stencil/core v1.8.4 Using "reflect".

garystupak avatar Jan 25 '20 15:01 garystupak

Same issue. Very annoying. stencil @ 1.8.5

Edit: For me, this issue is only present for the top level component and doesn't happen in child components. I have shadow-dom disabled.

starikcetin avatar Feb 03 '20 12:02 starikcetin

Same issue (versions 1.8.0 - 1.8.5). Works properly in @stencil/core v1.8.7 (using either 'reflect' or 'reflectToAttr')

brian-cruickshank avatar Feb 05 '20 18:02 brian-cruickshank

This is strange, I did not change anything, but it began to work.

garystupak avatar Feb 06 '20 10:02 garystupak

This is strange, I did not change anything, but it began to work.

Maybe you installed an update due to NPM version ranges.

starikcetin avatar Feb 09 '20 10:02 starikcetin

I'm still experiencing the issue with 1.8.11.

hnipps avatar Mar 04 '20 13:03 hnipps

In case anyone is struggling with this here's my workaround:

@Component({
  tag: "my-component",
  styleUrl: "my-component.css",
  shadow: true
})
export class MyComponent {
  @Prop() myAttr: string;
  @Element() host: HTMLElement;

  updateAttribute(someValue) {
    this.host.setAttribute("my-attr", someValue);
  }

  render() {
    // Whatever JSX...
  }
}

hnipps avatar Mar 04 '20 13:03 hnipps

I have a repo showing this as well: https://github.com/kensodemann/test-property

That repo is using @stencil/[email protected] and it works fine. But if you upgrade @stencil/core to 1.8.9 then the foo property stops being reflected.

kensodemann avatar Mar 17 '20 22:03 kensodemann

This should be fixed!

manucorporat avatar May 06 '20 20:05 manucorporat

In case of boolean , you need to set attribute and remove attribute

monir-a-khan avatar Jan 07 '22 14:01 monir-a-khan

Using @stencil/[email protected] and it also happened when deployed as web components in gcloud but somehow works locally

henryongsparkjoy avatar Mar 30 '22 04:03 henryongsparkjoy

In version @stencil/[email protected], I still have the same issue. Even though reflect is set to true for a Prop, the value is not updated in the DOM. I used @hnipps workaround.

milanbargiel avatar Jun 15 '23 14:06 milanbargiel