2

I want to compare old and new parameter values and decide to execute different initializations or logic based on that. How to do in Blazor?

2 Answers 2

1

// It's internal because there isn't a known use case for user code comparing // ParameterView instances, and even if there was, it's unlikely it should // use these equality rules which are designed for their effect on rendering.

Source: https://github.com/aspnet/AspNetCore/blob/master/src/Components/Components/src/ParameterView.cs (line 113)

Read the rest of the comment.
Note: ParameterView is the formerly known "object" ParameterCollection.

Now if this feature is essential to you, you should go to github and tell them that:

It's internal because there isn't a known use case for user code

is no longer true.

Of course you can store old values in variables in the OnParametersSetAsync lifecycle method and compared it with new parameter values, but then this is only a hacky work-around, and you should be careful about what you do. Surprises are awaiting you on each corner.

See this if you've got spare time: https://github.com/aspnet/AspNetCore/blob/3148acfb105a16aa6c535d00eb0e50ec03992f3f/src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs

Hope this helps...

0

You can store previous value on var, for example:

@page "/Counter"
@page "/Counter/{Id}"
<h1>Old Id: @IdSOld;</h1>
<a href="/Counter"> go home </a> |
<a href="/Counter/1"> go home 1 </a> |
<a href="/Counter/2"> go home 2 </a>

@code
{
    private string IdOld;
    [Parameter] public string Id { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        // copy par value somewhere
        IdOld = Id?.ToString() ?? "";

        // reset parm (set to null) by yourself
        Id=null;  
    }
}

See also: Blazor route changes in same page

1
  • Are you sure this works? The id at that point is the new I'd. It certainly doesn't work for components that are not page.
    – mz1378
    Commented Aug 16, 2019 at 22:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.