Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Props are immutable by default but still able to manipulate without mentioning mutable: true #2433

Closed
kskeerthi opened this issue May 13, 2020 · 2 comments · Fixed by #2779
Closed

Comments

@kskeerthi
Copy link

kskeerthi commented May 13, 2020

Been stuck where documentation refers props are immutable by nature but explicitly we can set them mutable by assigning mutable:true, but in my case i can still change the prop value without setting mutable property,

import { Component, ComponentInterface, h, Prop} from '@stencil/core';

@Component({
  tag: 'app-button',
  styleUrl: 'app-button.css'
  shadow: true,
})
export class AppButton implements ComponentInterface {
@Prop() message : string = "Hello Everyone!!"
componentDidLoad(){
  
  this.message ="kkkk"
  console.log(this.message)
}
render() {
    return (
      <p>{this.message}</p>
    );
    }

}

Instead of showing "Hello Everyone!!" as output it shows kkkk

@ionitron-bot ionitron-bot bot added the triage label May 13, 2020
@gustavando
Copy link

I have the same issue.

Stencil version:

@stencil/core@2.0.1

@claviska claviska added bug and removed triage labels Dec 7, 2020
@claviska
Copy link
Contributor

claviska commented Dec 7, 2020

Here's another example:

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

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.css',
  shadow: true,
})
export class MyComponent {
  @Prop({ mutable: true }) isMutable = 'is mutable';
  @Prop({ mutable: false }) isNotMutable = 'is not mutable';

  render() {
    return (
      <div>
        <p>
          Is Mutable: {this.isMutable}
          <br />
          Is Not Mutable: {this.isNotMutable}
        </p>
        <button type="button" onClick={() => (this.isMutable = 'foo')}>
          Set isMutable to 'foo'
        </button>
        <button type="button" onClick={() => (this.isNotMutable = 'bar')}>
          Set isNotMutable to 'bar'
        </button>
      </div>
    );
  }
}

I don't believe Stencil can actually enforce immutability, but you should see a warning in dev mode when the immutable prop is changed. This doesn't appear to be happening in 2.3.0.

See also #791, #1927.

claviska added a commit that referenced this issue Jan 5, 2021
* fix(): show warning when immutable props change (fixes #2433)

* test(): add tests for immutable prop warning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants