Skip to content

unexpected behavior with componentWillRender #2061

Closed

Description

@vidarc

Stencil version:

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:
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.
Screen Shot 2019-12-04 at 10 00 10 PM

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.

Steps to reproduce:

Related code:

Repo with example code: https://github.com/vidarc/stencil-bug

Activity

  1. dgibson666 commented on Dec 9, 2019

    @dgibson666

    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.

  2. frankelavsky commented on Jan 24, 2020

    @frankelavsky

    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:

      @State() expensiveWidth: number = 40
      @State() expensiveHeight: number = 40
      changeDimensions() {
        console.log("changing dimensions",75,50)
        this.expensiveWidth = 75
        this.expensiveHeight = 50
      }
    
      render() {
        return (<div>
          <div>
            <button
              onClick={() => {
                this.changeDimensions();
              }}
            >
              Change Dimensions
            </button>
          </div>
          <expensive-component
            width={this.expensiveWidth}
            height={this.expensiveHeight}
          >
          </expensive-component>
        </div>)}

    inside expensive-component.tsx:

      @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>
          <svg 
              width={this.width*2}
              height={this.height*2}
          >
            <rect
              width={this.width*2}
              height={this.height*2}
              fill="lightblue"
            ></rect>
            <circle
              r={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 expected
    console.log("changing dimensions",75,50) // changing dimensions 75 50
    // width watcher runs next, as expected
    console.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 values
    console.log("status of props in didUpdate",this.width,this.height) // status of props in didUpdate 75 50

    Screen Shot 2020-01-24 at 4 28 33 PM

    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:

      @Watch('width')
      widthWatcher(_new, _old) {
        this.expensiveCalculation()
      }
    
      @Watch('height')
      heightWatcher(_new, _old) {
        this.expensiveCalculation()
      }

    This is the rendered output at the beginning:
    Screen Shot 2020-01-24 at 11 51 50 AM

    The rendered output after pressing the button:
    Screen Shot 2020-01-24 at 11 53 36 AM

    The expected output:
    Screen Shot 2020-01-24 at 11 53 52 AM

    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.

    <expensive-component>
      height={this.expensiveHeight}
      width={this.expensiveWidth}
    </expensive-component>

    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...).

  3. tricki commented on Jul 4, 2020

    @tricki
    Contributor

    Apparently the order of the lifecycle methods when changing two properties (prop1 and prop2) from 'old' to 'new' is:

    • Watcher for prop1 fires (this.prop1 === 'new' && this.prop2 === 'old')
    • shouldUpdate fires (this.prop1 === 'new' && this.prop2 === 'old')
    • willUpdate fires (this.prop1 === 'new' && this.prop2 === 'old')
    • willRender fires (this.prop1 === 'new' && this.prop2 === 'old')
    • Watcher for prop2 fires (this.prop1 === 'new' && this.prop2 === 'new')
    • render() fires (this.prop1 === 'new' && this.prop2 === 'new')

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions