blazorblazor-webassemblyblazor-component

Blazor is setting component parameter every time I interact with component


What I want to achieve is to call a function every time Parameter value of a component is updated, So I created a component with a Parameter and call a method inside its setter like this.

private LookupConfig _lookupConfig;

Parameter]
public LookupConfig Config { get { return _lookupConfig; } set { _lookupConfig = value; Console.WriteLine("Parameter Setter"); InitializeDropdownValues(); } }

In my parent component, I have a property.

public LookupConfig CustomerDropdown = new LookupConfig { LookupType = LookupType.Customer };

and I am passing it to child component like this.

<NeeleezAutoComplete Config="CustomerDropdown"/>

but every time I interact with NeeleezAutoComplete component setter of Parameter property Config is get called.

is it intentional behaviour of blazor parameters am i missing something ? and how do i achieve this functionlaity?


Solution

  • is it intentional behaviour of blazor parameters

    Yes, this is by design. Blazor tracks parameter changes very lightly.

    The solution seems simple:

    set 
    { 
      if (_lookupConfig == value) return;   // no change? Out of here. 
      _lookupConfig = value; 
      Console.WriteLine("Parameter Setter"); 
      InitializeDropdownValues(); 
    }
    

    You may want to check Equality for LookupConfig but normally the defaults should suffice.