Loader
Quick start
You can use the Loader component to show a loading indicator while waiting for data to load and display a message when an error occurs.
Global templates configuration
You can configure the global templates for the loader component using the BdkLoaderOptions
class.
The following example shows how to configure the global templates for the loader component.
BdkLoaderOptions.Loading.ChangeContent<LoadingContent>();
<div class="d-flex flex-column align-items-center justify-content-center">
<div class="spinner-border text-danger" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<div class="mt-3">@(string.IsNullOrEmpty(Message) ? "Loading..." : Message)</div>
</div>
@code
{
[Parameter] public string Message { get; set; } = string.Empty;
}
The component created to be used as LoadingContent needs to have a parameter called Message of type string.
You can register a RenderFragment string or MarkupString in place of the component type, noting that only MarkupString does not support receiving the Message defined in the BdkLoader component.
Similarly, the registration of the component type to be used in error cases needs to have a parameter called ErrorResult of type BdkLoaderErrorResult, which can be used to access the exception that occurred and check if the retry button will be displayed. Below is an example of the registration and usage of this component:
BdkLoaderOptions.Error.RegisterContent<Exception, ErrorContentExample>();
You can register an ErrorContent for a specific exception type.
<div class="d-flex flex-column align-items-center justify-content-center">
<div class="mt-3 text-danger">
@ErrorResult.Exception.Message
</div>
@if (ErrorResult.Loader.CanRetry)
{
<div class="mt-3">
<button class="btn btn-primary" @onclick="ErrorResult.Loader.ReloadAsync">@ErrorResult.Loader.CanRetryTitle</button>
</div>
}
</div>
@code
{
[Parameter] public required BdkLoaderErrorResult ErrorResult { get; set; }
}
Basic
- 04415632-8387-4c66-a0c3-a07625ca332e - 12/22/2024 23:09:42 +00:00
- b1267643-3f28-4db9-aba2-be1e6694891c - 12/22/2024 23:09:42 +00:00
- ccc5e48a-8c19-4c55-bca3-16f3a05fb4fe - 12/22/2024 23:09:42 +00:00
- fe1bfe36-24eb-4311-b110-b02ba9305521 - 12/22/2024 23:09:42 +00:00
- 7937a442-aa2c-4e9f-a985-ea73fcd61922 - 12/22/2024 23:09:42 +00:00
@inject ISubjectService Service
<BdkLoader Load="Service.ListAsync">
<ul>
@foreach (var item in context)
{
<li>@item.Id - @item.CreatedOn</li>
}
</ul>
</BdkLoader>
Basic: no result
Attention: the BdkLoader context will be null, this mode can be used when a return value is not necessary.
Content displayed after loading
@inject ISubjectService Service
<BdkLoader T="object?" Load="LoadAsync">
<p>
Content displayed after loading
</p>
</BdkLoader>
@code
{
private async Task<object?> LoadAsync()
{
await Service.ProcessAsync();
return null;
}
}
Basic with custom loading message
- 7038cc96-d764-4220-852c-c9ce918d59c5 - 12/22/2024 23:09:42 +00:00
- 5299b62f-0120-43c0-9be5-85472ef25169 - 12/22/2024 23:09:42 +00:00
- 5d362530-e024-4b11-bc7d-48e57f689986 - 12/22/2024 23:09:42 +00:00
- 505636a2-cc66-4ec3-b7d8-d8c88a69bab6 - 12/22/2024 23:09:42 +00:00
- fc3d2d6e-2f9a-4747-9baf-6e758bf28f47 - 12/22/2024 23:09:42 +00:00
@inject ISubjectService Service
<BdkLoader Load="Service.ListAsync" Message="This is a custom message">
<ul>
@foreach (var item in context)
{
<li>@item.Id - @item.CreatedOn</li>
}
</ul>
</BdkLoader>
Basic with error
@inject ISubjectService Service
<BdkLoader Load="LoadAsync">
<ul>
@foreach (var item in context)
{
<li>@item.Id - @item.CreatedOn</li>
}
</ul>
</BdkLoader>
@code
{
private async Task<ResponseItem[]> LoadAsync()
{
await Service.ProcessAsync(throwErrorAfterProcess: true); // Simulate error
return await Service.ListAsync();
}
}
Basic with error and CanRetry
@inject ISubjectService Service
<BdkLoader Load="LoadAsync" CanRetry CanRetryTitle="Retry">
<ul>
@foreach (var item in context)
{
<li>@item.Id - @item.CreatedOn</li>
}
</ul>
</BdkLoader>
@code
{
private async Task<ResponseItem[]> LoadAsync()
{
await Service.ProcessAsync(throwErrorAfterProcess: true); // Simulate error
return await Service.ListAsync();
}
}
Basic with error, CanRetry and local custom ErrorContent
@inject ISubjectService Service
<BdkLoader Load="LoadAsync" CanRetry CanRetryTitle="Retry">
<ErrorContent Context="errorResult">
<div class="d-flex flex-column align-items-center justify-content-center">
<div class="mt-3 text-info">
@errorResult.Exception.Message
</div>
@if (errorResult.Loader.CanRetry)
{
<div class="mt-3">
<button class="btn btn-danger" @onclick="errorResult.Loader.ReloadAsync">@errorResult.Loader.CanRetryTitle</button>
</div>
}
</div>
</ErrorContent>
<ChildContent>
Content here
</ChildContent>
</BdkLoader>
@code
{
private async Task<ResponseItem[]> LoadAsync()
{
await Service.ProcessAsync(throwErrorAfterProcess: true); // Simulate error
return await Service.ListAsync();
}
}
Basic with PreserveState
When the PreserveState mode is enabled, the BdkLoader will attempt to preserve the result of the Load function, avoiding going back to the server and causing an unwanted “Reload.”
- Item: 5139ff3d-2a1c-486a-a4d7-259b3041e5c4
- Item: 04dd2002-5047-4289-9842-b2c329184bf9
- Item: 158eb05b-35d2-47f2-9357-596e5aa48ec3
- Item: 9565dea1-8ee3-4cf1-bfab-f1e18a0d6503
- Item: 82f41b66-a72c-4e9a-8c9b-fa3d880bed59
@inject ISubjectService Service
<BdkLoader Load="Service.ListAsync" PreserveState >
<ul>
@foreach (var result in context)
{
<li>Item: @result.Id</li>
}
</ul>
</BdkLoader>
Basic with PreserveState and not result
Content displayed after loading
@inject ISubjectService Service
<BdkLoader T="object?" Load="LoadAsync" PreserveState>
<p>
Content displayed after loading
</p>
</BdkLoader>
@code
{
private async Task<object?> LoadAsync()
{
await Service.ProcessAsync();
return null;
}
}