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.
- 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!
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>
Some notes on the example above
- 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.
Click here to go to the full documentation
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.
Any contributions are welcome, but ideally start by creating an issue.
- 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.