Blazor wasm:从顶层组件传递RouteData到内部组件会触发ParameterSet两次

huangapple go评论60阅读模式
英文:

Blazor wasm: cascading RouteData from top component to inner component fires ParameterSet twice

问题

上周,尽管我不是Blazor专家,但我被要求帮助处理一个Blazor Wasm应用程序,当应用程序导航到具有至少一个查询字符串参数的URI时,它会两次触发SetParametersAsync方法(如果至少有一个参数,则会触发ParametersSetAsync方法两次)。

首先,让我提供关于该应用程序以及为何此行为有问题的更多信息。为了支持资源URI共享,大多数操作会生成一个URI,应用程序会导航到该URI。在实践中,这意味着大多数操作最终会导航到一个URI,该URI上存在多个参数,这些参数从URI上的查询字符串参数中提取。

例如,假设您有一个页面(/demo),显示一些可以通过几个字段(比如 ABC)进行筛选的项目列表。每当用户单击筛选按钮时,将生成一个新的URI(/demo?A=...&B=...&C=...),并将其传递给NavigationManager.NavigateTo。这将导致调用SetParametersAsync方法,该方法负责获取信息并向用户显示它(在此情况下,它将调用多个Web服务以获取所需的数据)。

经过一些测试,我注意到存在问题的页面(即SetParametersAsync方法被调用两次的页面)是通过从顶级组件传递的CascadeParameter获得RouteData对象的实例。

为了重现这种行为,您只需要创建一个新的Blazor WebAssembly应用程序,并将默认生成的代码更改为以下内容:

@*App.razor*@

<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
        <CascadingValue Value="@routeData">
            <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
            <FocusOnNavigate RouteData="@routeData" Selector="h1"/>
        </CascadingValue>
    </Found>
    ...
</Router>
@*Index.razor*@

@page "/"
@inject NavigationManager NavigationManager

<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?"/>
<Button @onclick="Navigate">Click me</Button>

@code {
    [CascadingParameter]
    public RouteData? RouteData { get; set; }

    [SupplyParameterFromQuery(Name = "in")]
    [Parameter]
    public string Info { get; set; }

    private void Navigate() {
        var uri = NavigationManager.GetUriWithQueryParameter("in", DateTime.Now.ToString());
        NavigationManager.NavigateTo(uri);
    }

    public override async Task SetParametersAsync(ParameterView parameters) {
        await base.SetParametersAsync(parameters);
        Console.WriteLine($"Navigation: {Info}");
    }
}

如果运行上述示例,您会注意到级联RouteData会混乱事情,该方法将使用通过URI传递的日期进行调用,然后使用空日期进行调用(我访问属性而不是直接查询字符串值)。如果没有级联RouteData参数,那么一切都会正常运行。

我尝试搜索文档,但没有找到任何说明不应该使用级联RouteData参数的内容。诚然:我不是Blazor专家,我也没有在我的应用程序中需要级联RouteData(在这种情况下,可能也不需要),但我真的希望有人能确认这种行为,并且如果可能的话,指向微软的官方文档,以解释这种行为。

谢谢。

英文:

Last week, amd even though I'm no Blazor expert, I was called to help with a Blazor Wasm app that was firing the SetParametersAsync method twice when the app navigates to an URI that has at least one querystring parameter (if there's at least one, then it will end up firing the ParametersSetAsyncmethod twice).

First, let me give some more info about the app and why this behavior is problematic. In order to support resource uri sharing, most actions generates an uri to which the app navigates to. In practice, this means that most actions end up navigating to an uri, which is handled by a page that has several parameters that are fed from the querystring parameters present on the URI.

For instance, suppose you have a page (/demo) that shows a list of items which can be filtered by a couple of fields (lets say A, B, C). Whenever a user clicks the filter button, a new URI is generated (/demo?A=...&amp;B=...&amp;C=...) and its passed the NavigationManager,NavigateTo. This ends up calling the SetParametersAsync method which is responsible for getting the information and showing it to the user (in this case, it will call several web services in order to get the required data).

After some testing, I've noticed that the problematic pages (ie, those where the SetParametersAsync method is called twice) are getting an instance of the RouteData object through a CascadeParameter which is being passed from the top component.

In order to reproduce the behavior, you just need to create a new Blazor WebAssemly App and change the default generated code to the following:

@*App.razor*@

&lt;Router AppAssembly=&quot;@typeof( App ).Assembly&quot;&gt;
    &lt;Found Context=&quot;routeData&quot;&gt;
        &lt;CascadingValue Value=&quot;@routeData&quot;&gt;
            &lt;RouteView RouteData=&quot;@routeData&quot; DefaultLayout=&quot;@typeof( MainLayout )&quot;/&gt;
            &lt;FocusOnNavigate RouteData=&quot;@routeData&quot; Selector=&quot;h1&quot;/&gt;
        &lt;/CascadingValue&gt;
    &lt;/Found&gt;
    ...
&lt;/Router&gt;

@*Index.razor*@

@page &quot;/&quot;
@inject NavigationManager NavigationManager

&lt;PageTitle&gt;Index&lt;/PageTitle&gt;
&lt;h1&gt;Hello, world!&lt;/h1&gt;
Welcome to your new app.
&lt;SurveyPrompt Title=&quot;How is Blazor working for you?&quot;/&gt;
&lt;Button @onclick=&quot;Navigate&quot;&gt;Click me&lt;/Button&gt;

@code {
    [CascadingParameter]
    public RouteData? RouteData { get; set; }

    [SupplyParameterFromQuery(Name = &quot;in&quot;)]
    [Parameter]
    public string Info { get; set; }

    private void Navigate() {
        var uri = NavigationManager.GetUriWithQueryParameter(&quot;in&quot;, DateTime.Now.ToString( ));
        NavigationManager.NavigateTo(uri);
    }

    public override async Task SetParametersAsync(ParameterView parameters) {
        await base.SetParametersAsync(parameters);
        Console.WriteLine($&quot;Navigation: {Info}&quot;);
    }
}

If you run the previous sample, you'll notice that cascading the RouteData tends to mess things up and that the method will be called with the date that was passed through the uri and then with an empty date (I'm accessing the property, not the querystring value directly). If there's no RouteData parameter being cascaded, then everything runs just fine.

I've tried searching the docs but haven't found anything which says that cascading shouldn't be used with RouteData parameters. Granted: I'm not a Blazor wizard and I haven't needed to cascade the RouteData in my apps (and in this case, it probably wasn't required either), but I'd really love that someone could confirm this behavior and, if possible, point me to some official document from MS that justifies this behavior.

Thanks.

答案1

得分: 0

SetParamnetersAsync isn't being called twice, the component is being rendered twice. Subtle different.

这个函数并没有被调用两次,而是组件被渲染了两次。细微的不同。

英文:

SetParamnetersAsync isn't being called twice, the component is being rendered twice. Subtle different.

This will take a little explaining, but to show that it's nothing to do with routes here's my demo App that does the same thing:

&lt;Router AppAssembly=&quot;@typeof(App).Assembly&quot;&gt;
    &lt;Found Context=&quot;routeData&quot;&gt;
        &lt;CascadingValue Value=&quot;@value&quot;&gt;
            &lt;RouteView RouteData=&quot;@routeData&quot; DefaultLayout=&quot;@typeof(MainLayout)&quot; /&gt;
            &lt;FocusOnNavigate RouteData=&quot;@routeData&quot; Selector=&quot;h1&quot; /&gt;
        &lt;/CascadingValue&gt;
    &lt;/Found&gt;
    &lt;NotFound&gt;
        &lt;PageTitle&gt;Not found&lt;/PageTitle&gt;
        &lt;LayoutView Layout=&quot;@typeof(MainLayout)&quot;&gt;
            &lt;p role=&quot;alert&quot;&gt;Sorry, there&#39;s nothing at this address.&lt;/p&gt;
        &lt;/LayoutView&gt;
    &lt;/NotFound&gt;
&lt;/Router&gt;

@code {
    public string value =&gt; $&quot;{DateTime.Now.ToLongTimeString()}&quot;;
}

Here's my Index with aome extra logging:

@page &quot;/&quot;
@inject NavigationManager NavigationManager

&lt;PageTitle&gt;Index&lt;/PageTitle&gt;
&lt;div class=&quot;m2 p-2&quot;&gt;
    &lt;Button @onclick=&quot;Navigate&quot;&gt;Click me&lt;/Button&gt;
&lt;/div&gt;

&lt;div class=&quot;bg-dark text-white&quot;&gt;
    &lt;pre&gt;@_log.ToString()&lt;/pre&gt;
&lt;/div&gt;

@code {

    [CascadingParameter] public string? Data { get; set; }

    [SupplyParameterFromQuery(Name = &quot;in&quot;)]
    [Parameter] public string Info { get; set; }

    private System.Text.StringBuilder _log = new();

    private void Navigate()
    {
        var uri = NavigationManager.GetUriWithQueryParameter(&quot;in&quot;, DateTime.Now.ToString());
        NavigationManager.NavigateTo(uri);
    }

    public override async Task SetParametersAsync(ParameterView parameters)
    {
        parameters.SetParameterProperties(this);
        var message = $&quot;Render at {DateTime.Now.ToLongTimeString()}&quot;;
        _log.AppendLine(message);
        Console.WriteLine(message);

        await base.SetParametersAsync(ParameterView.Empty);
        var navMessage = $&quot;Navigation: {Info}&quot;;
        _log.AppendLine($&quot;{DateTime.Now.ToLongTimeString()} : {navMessage}&quot;);
        Console.WriteLine($&quot;{DateTime.Now.ToLongTimeString()} : {navMessage}&quot;);
    }
}

Here's the log with the key element highlighted. Note the Render time verses the Navigation time. The render occured with the old data?

Blazor wasm:从顶层组件传递RouteData到内部组件会触发ParameterSet两次

The reason is that when the Route component renders, the renderer knows that Index has a reference to the cascade and the cascaded value has changed. It therefore calls SetParametersAsync on Index which renders the component. It then renders RouteView which renders Index (again), this time with the new data.

It's one of the side effects of cascading objects, and ComponentBase's render logic. The render doesn't know if an object has changed, so everytime it renders the owning component, all the cascade capturing components also get rendered. If you look through the Microsoft code base you will see that most cascades are set to IsFixed.

In this instance I would probably use an event driven model rather than the cascade. I've provided an example below.

The provider and EventArgs.

public class RouteDataProvider
{
    public event EventHandler&lt;RouteDataEventArgs&gt;? RouteChangd;

    public RouteData? RouteData { get; private set; }

    public void NotifyRouteChanged(RouteData routeData)
    {
        this.RouteData = routeData;
        this.RouteChangd?.Invoke(this, new(routeData));
    }
}

public class RouteDataEventArgs : EventArgs
{
    public RouteData RouteData { get; set; }
    public RouteDataEventArgs(RouteData routeData)
        =&gt; this.RouteData = routeData;
}

Updated App with a fixed cascade.

&lt;Router AppAssembly=&quot;@typeof(App).Assembly&quot;&gt;
    &lt;Found Context=&quot;routeData&quot;&gt;
        &lt;CascadingValue Value=_routeDataProvider IsFixed&gt;
            @{
                _routeDataProvider.NotifyRouteChanged(routeData);
            }
            &lt;RouteView RouteData=&quot;@routeData&quot; DefaultLayout=&quot;@typeof(MainLayout)&quot; /&gt;
            &lt;FocusOnNavigate RouteData=&quot;@routeData&quot; Selector=&quot;h1&quot; /&gt;
        &lt;/CascadingValue&gt;
    &lt;/Found&gt;
    &lt;NotFound&gt;
        &lt;PageTitle&gt;Not found&lt;/PageTitle&gt;
        &lt;LayoutView Layout=&quot;@typeof(MainLayout)&quot;&gt;
            &lt;p role=&quot;alert&quot;&gt;Sorry, there&#39;s nothing at this address.&lt;/p&gt;
        &lt;/LayoutView&gt;
    &lt;/NotFound&gt;
&lt;/Router&gt;

@code {
    private readonly RouteDataProvider _routeDataProvider = new();
}

And Index:

@page &quot;/&quot;
@inject NavigationManager NavigationManager

&lt;PageTitle&gt;Index&lt;/PageTitle&gt;
&lt;div class=&quot;m2 p-2&quot;&gt;
    &lt;Button @onclick=&quot;Navigate&quot;&gt;Click me&lt;/Button&gt;
&lt;/div&gt;

&lt;div class=&quot;bg-dark text-white&quot;&gt;
    &lt;pre&gt;@_log.ToString()&lt;/pre&gt;
&lt;/div&gt;

@code {

    [CascadingParameter] public RouteDataProvider? _routeDataProvider { get; set; }

    [SupplyParameterFromQuery(Name = &quot;in&quot;)]
    [Parameter] public string Info { get; set; }

    private System.Text.StringBuilder _log = new();

    private void Navigate()
    {
        var uri = NavigationManager.GetUriWithQueryParameter(&quot;in&quot;, DateTime.Now.ToString());
        NavigationManager.NavigateTo(uri);
    }

    public override async Task SetParametersAsync(ParameterView parameters)
    {
        parameters.SetParameterProperties(this);
        var message = $&quot;Render at {DateTime.Now.ToLongTimeString()}&quot;;
        _log.AppendLine(message);
        _log.AppendLine($&quot;Route Component: {_routeDataProvider?.RouteData?.PageType.FullName ?? &quot;Not defined&quot;}&quot;);
        Console.WriteLine(message);

        await base.SetParametersAsync(ParameterView.Empty);
        var navMessage = $&quot;Navigation: {Info}&quot;;
        _log.AppendLine($&quot;{DateTime.Now.ToLongTimeString()} : {navMessage}&quot;);
        Console.WriteLine($&quot;{DateTime.Now.ToLongTimeString()} : {navMessage}&quot;);
    }
}

huangapple
  • 本文由 发表于 2023年5月15日 02:57:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定