Phetch.Blazor
1.0.0
dotnet add package Phetch.Blazor --version 1.0.0
NuGet\Install-Package Phetch.Blazor -Version 1.0.0
<PackageReference Include="Phetch.Blazor" Version="1.0.0" />
paket add Phetch.Blazor --version 1.0.0
#r "nuget: Phetch.Blazor, 1.0.0"
// Install Phetch.Blazor as a Cake Addin #addin nuget:?package=Phetch.Blazor&version=1.0.0 // Install Phetch.Blazor as a Cake Tool #tool nuget:?package=Phetch.Blazor&version=1.0.0
Phetch
Documentation | API Reference | Sample app
Phetch is a small Blazor library for handling async query state, in the style of React Query, SWR, or RTK Query.
Currently, Phetch is only designed for use with Blazor WebAssembly. However, the core package (Phetch.Core) has no dependencies on Blazor or ASP.NET Core, so in theory it can be used anywhere that supports .NET Standard 2.1.
Features
- Automatically handles loading and error states, and updates your components whenever the state changes.
- Automatically caches data returned by queries, and makes it easy to invalidate or update this cached data when needed.
- Supports calling any async method as a query (not restricted just to HTTP requests).
- Supports pagination, prefetching, request de-duplication, retries, dependent queries, CancellationTokens, and more.
- 100% strongly typed, with nullability annotations.
- Lightweight and easy to combine with other state management methods.
- No Javascript needed!
Show me some code!
Click here to view the source code for the sample project, with more detailed examples.
Below is the code for a basic component that runs a query when the component is first loaded. Phetch can do a whole lot more than that though, so make sure to check out the samples project and full documentation!
Defining an endpoint:
using Phetch.Core;
// This defines an endpoint that takes an int and returns a bool.
var isEvenEndpoint = new Endpoint<int, bool>(
// Replace this part with your own async function:
async (value, cancellationToken) =>
{
var response = await httpClient.GetFromJsonAsync<dynamic>(
$"https://api.isevenapi.xyz/api/iseven/{value}",
cancellationToken);
return response.IsEven;
}
);
Using the endpoint in a component:
@using Phetch.Blazor
<UseEndpoint Endpoint="isEvenEndpoint" Arg="3" Context="query">
@if (query.IsError) {
<p>Something went wrong!</p>
} else if (query.IsLoading) {
<p>Loading...</p>
} else if (query.HasData) {
<p>The number is @(query.Data ? "even" : "odd")</p>
}
</UseEndpoint>
<details> <summary>Some notes on the example above</summary>
- Inside the
<UseEndpoint>
component, you can usequery
to access the current state of the query. Changing theContext
parameter will rename this object. - By changing the
Arg
parameter, the query will automatically be re-fetched when needed. - Normally, you would share endpoints around your application using dependency injection (see Defining Query Endpoints).
- If you need to access the query state inside the
@code
block of a component, you can replace<UseEndpoint/>
with the pattern described in Using Query Objects Directly.
</details> </br>
Click here to go to the full documentation
Installing
You can install Phetch via the .NET CLI with the following command:
dotnet add package Phetch.Blazor
If you're using Visual Studio, you can also install via the built-in NuGet package manager.
Contributing
Any contributions are welcome, but ideally start by creating an issue.
Comparison with other libraries
- Fluxor, Blazor-State or Cortex.Net: These are general-purpose state management libraries, so:
- They will give you more control over exactly how your state is updated, and will work for managing non-query state.
- However, for managing query state, you will need much more code to achieve the same things that Phetch can do in just a couple of lines. This becomes particularly important if you need to cache data or share queries across components.
- Fusion: This is a much larger library, focused on real-time updates. Fusion is great if your app has lots of real-time functionality, but for most applications it will probably be overkill compared to Phetch.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.4)
- Phetch.Core (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 759 | 7/13/2024 |
0.7.1 | 1,581 | 6/8/2024 |
0.7.0 | 615 | 5/4/2024 |
0.6.0 | 697 | 12/1/2023 |
0.5.1 | 345 | 7/14/2023 |
0.5.0 | 229 | 6/19/2023 |
0.4.0 | 283 | 3/6/2023 |
0.3.1 | 372 | 11/19/2022 |
0.3.0 | 376 | 11/13/2022 |
0.2.0 | 432 | 8/18/2022 |
0.1.2 | 475 | 8/6/2022 |
0.1.1 | 424 | 8/1/2022 |
0.1.0 | 470 | 7/26/2022 |
0.0.3 | 489 | 7/9/2022 |