You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
unexpected behavior with componentWillRender #2061
Current behavior:
Changing various props in sequential lines, does not produce expected results with componentWillRender and shouldComponentUpdate.
What I am doing is updating the props, first, middle, and last, inside a click handler. The change to first triggers the componentWillRender lifecycle as I would expect, as well as the componentShouldRender lifecycle. However, you do not get those for the other two prop changes, even though they are properly changed by the time you get to the render method.
Logs of what is happening.
Expected behavior:
I would expect either the prop changes to be batched somehow, seeing as they are modified very quickly, or for there to be some way to capture all the changes before the render method.
In my actual code at work, I need to compute some data before render based upon two props. Would it just be better to always do that type of stuff inside the render method then? Seems to be the only place I have a guarantee to their values right now, but worry it might be costly.
If you stuff these updated values into a temp object and use destructuring to assign them, does it do them sequentially as well or all at once? I'm curious - haven't tried this myself.
I'm having similarly unexpected behavior in componentWillUpdate, using "@stencil/core":"1.7.0".
It appears as though componentWillUpdate only has the state of props based on the first prop change it receives from the render function that calls it.
I've built an example using an application with a button that can be pressed which will change two props that are sent to an <expensive-component>. This component will make an expensive calculation using these two props before rendering (my example below is just demonstrative, imagine that I am doing more than just finding the min between two values but a lot of math and iteration over data). I expect there would be a way to intercept both prop changes in order to make a SINGLE call to the expensiveCalculation. This does not seem possible.
@Prop({mutable: true})height;
@Prop({mutable: true})width;
shouldCalculateLayout:boolean=false;
maximumDiameter: number;
@Watch('width')widthWatcher(_new,_old){console.log("width changing",_old,_new)this.shouldCalculateLayout=true}
@Watch('height')heightWatcher(_new,_old){console.log("height changing",_old,_new)this.shouldCalculateLayout=true}componentWillLoad(){this.expensiveCalculation()}componentWillUpdate(){if(this.shouldCalculateLayout){console.log("status of props in willUpdate",this.width,this.height)this.expensiveCalculation()this.shouldCalculateLayout=false}}componentDidUpdate(){console.log("status of props in didUpdate",this.width,this.height)}expensiveCalculation(){// this example is purely demonstrative!this.maximumDiameter=Math.min(this.width,this.height)console.log("max diameter is",this.maximumDiameter)// now expensive structuring is calculated using diameter}render(){return(<div><svgwidth={this.width*2}height={this.height*2}><rectwidth={this.width*2}height={this.height*2}fill="lightblue"></rect><circler={this.maximumDiameter}cx={this.width}cy={this.height}fill="darkred"></circle></svg></div>)}
The console's logs are ordered as follows, after the user clicks the button in the application:
// button's console runs first, as expectedconsole.log("changing dimensions",75,50)// changing dimensions 75 50// width watcher runs next, as expectedconsole.log("width changing",_old,_new)// width changing 40 75// componentWillUpdate runs next, NOT expected!// height has not received its new value yet!console.log("status of props in willUpdate",this.width,this.height)// status of props in willUpdate 75 40// expensiveCalculation runs next, without updated height (producing unexpected results)console.log("max diameter is",this.maxDiameter)// max diameter is 40// height watcher now runs, NOT expected!console.log("height changing",_old,_new)// height changing 40 50// render now happens, which produces *unexpected* dimensions in the <circle> and expected dimensions in the <rect> and <svg>// componentDidUpdate runs, as expected and has all expected prop valuesconsole.log("status of props in didUpdate",this.width,this.height)// status of props in didUpdate 75 50
It appears that even though the button is a single block of javascript code which changes two different props, stencil's lifecycle begins with only one change recognized. I want to be able to intercept both height and width's changes before render, to avoid running my expensive layout calculations twice. The alternative I have right now (just to get this to work as expected) is:
An interesting finding: If I change the order of the props in my application's render function (swapping width and height), whichever prop is on top is the one that "wins" the race and is included in componentWillUpdate.
I would expect that there is a way to detect multiple prop changes when those changes take place in a single block of code. Right now we are dealing with some performance issues on a very heavy-lifting component because of this (imagine we have more than just two mathematically-dependent variables involved...).
Note that willUpdate and willRender are called before this.prop2 is changed.
Stencil Team: Is it expected behavior that watchers fire after willUpdate and willRender (and shouldRender) when changing multiple properties at once? I would have expected willRender to run just before the render.
Activity
dgibson666 commentedon Dec 9, 2019
If you stuff these updated values into a temp object and use destructuring to assign them, does it do them sequentially as well or all at once? I'm curious - haven't tried this myself.
frankelavsky commentedon Jan 24, 2020
I'm having similarly unexpected behavior in componentWillUpdate, using
"@stencil/core":"1.7.0".It appears as though componentWillUpdate only has the state of props based on the first prop change it receives from the render function that calls it.
I've built an example using an application with a button that can be pressed which will change two props that are sent to an
<expensive-component>. This component will make an expensive calculation using these two props before rendering (my example below is just demonstrative, imagine that I am doing more than just finding the min between two values but a lot of math and iteration over data). I expect there would be a way to intercept both prop changes in order to make a SINGLE call to the expensiveCalculation. This does not seem possible.My test example is below:
application's tsx:
inside expensive-component.tsx:
The console's logs are ordered as follows, after the user clicks the button in the application:
It appears that even though the button is a single block of javascript code which changes two different props, stencil's lifecycle begins with only one change recognized. I want to be able to intercept both height and width's changes before render, to avoid running my expensive layout calculations twice. The alternative I have right now (just to get this to work as expected) is:
This is the rendered output at the beginning:

The rendered output after pressing the button:

The expected output:

An interesting finding: If I change the order of the props in my application's render function (swapping width and height), whichever prop is on top is the one that "wins" the race and is included in componentWillUpdate.
I would expect that there is a way to detect multiple prop changes when those changes take place in a single block of code. Right now we are dealing with some performance issues on a very heavy-lifting component because of this (imagine we have more than just two mathematically-dependent variables involved...).
tricki commentedon Jul 4, 2020
Apparently the order of the lifecycle methods when changing two properties (
prop1andprop2) from'old'to'new'is:prop1fires (this.prop1 === 'new' && this.prop2 === 'old')this.prop1 === 'new' && this.prop2 === 'old')this.prop1 === 'new' && this.prop2 === 'old')this.prop1 === 'new' && this.prop2 === 'old')prop2fires (this.prop1 === 'new' && this.prop2 === 'new')render()fires (this.prop1 === 'new' && this.prop2 === 'new')Note that willUpdate and willRender are called before
this.prop2is changed.Stencil Team: Is it expected behavior that watchers fire after willUpdate and willRender (and shouldRender) when changing multiple properties at once? I would have expected willRender to run just before the render.