Let's say I have the following code in MainLayout.razor
<CascadingValue Value="@(async () => await someclass.GetValueAsnyc())" Name="asyncValue">
<article class="content px-4">
@Body
</article>
</CascadingValue>
And in Index.razor:
<h1>Hello!</h1>
@code{
[CascadingParameter(Name = "asyncValue")] public string AsyncValue{ get; set; }
protected override async Task OnInitializedAsync()
{
//do something with AsyncValue- it is always null
}
}
My assumption is that the declaration of the variable in Index.razor isn't awaiting the cascading value in Mainlayout.razor. It does work is I call the method synchronsly with .Result at the end, but that's not what I would like to do if possible.
Does anyone have any guidance or can point me in the right direction so that the cascading value loads asynchronously?
I think this should help.
First a consumer component. The cascaded value is a
Taskwhich may or may not have completed. To consume it youawaitit. If it's already completed the there's no awaiting to do. It provides the result immediately.And a test page. I haven't put it in a layout or App as it's easier to see what's happening in a test page. The cascaded value is a
Task<string>field that you assign a method to.This is basically how the AuthenticationState cascade works.