-
Notifications
You must be signed in to change notification settings - Fork 798
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
Immutable property values can be changed #791
Comments
i am not sure where it's stated that Props are immutable, they are not at all. I believe that's part of your responsibility to handle it. However, i like the idea of documenting good design patterns that help prevent this kind of bugs. You can calculate
or even better:
|
The Stencil docs seem to indicate that props are immutable:
If that is not really the case and they are always mutable then what is the purpose of the The simple example of calculating Examples from Ionic coreTaking a quick look at Ionic core it seems like there are many subtle cases where mutable props are not handled correctly. For example, in componentWillLoad() {
// By default, password inputs clear after focus when they have content
if (this.clearOnEdit === undefined && this.type === 'password') {
this.clearOnEdit = true;
}
} In this case the Problems can also exist in this component when using private property values. For example: didBlurAfterEdit = false; Here the value gets set when the component gets created and may later get changed to a different value. The problem occurs when the component is re-used with different property values. In this case I only looked at one component so there are probably many other subtle issues within ionic core. Potential changesThe heart of the issue is that an existing component gets re-used if the parent renders the same component tag in the same DOM sequence. This makes it tricky to handle any state information ( If the parent component is displaying a form with a bunch of child It is, unfortunately, not clear how you would distinguish re-rendering a component (where state should be preserved) vs. moving a component in the DOM (where state should be moved) other than by being entirely stateless. One possible improvement would be to enforce immutable property values as currently documented and create a new component instance if an immutable property value is changed vs. re-using an existing component if mutable property values are not changed. This would help with cases where "stale" state information might be re-used but would not handle preserving state when an element is moved. In any case I think the |
Yeah. I'd like to know that, too. |
Stencil version:
I'm submitting a:
[x] bug report
Current behavior:
When a component is rendered it is possible for immutable property values to change.
For example, the following component has properties and a state value that is calculated based on the properties.
This component gets rendered from a parent container and later gets rendered again with different property values.
During the first render the component behaves as expected:
After clicking on the button in the container component the child component is rendered again with changed property values:
In this case the child component is assuming immutable property values and thinks it is ok to continue to use a calculated value that was based on the property values. In the example, the
fullName
state value is not valid.The original instance of the child component is being re-used so the constructor and componentWillLoad methods are not called.
Expected behavior:
There are a couple of ways this could be handled:
Currently it is possible to end up with very tricky errors when making the false assumption that an immutable property value will not change.
The text was updated successfully, but these errors were encountered: