BlazorHooked 0.3.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package BlazorHooked --version 0.3.1                
NuGet\Install-Package BlazorHooked -Version 0.3.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlazorHooked" Version="0.3.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BlazorHooked --version 0.3.1                
#r "nuget: BlazorHooked, 0.3.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BlazorHooked as a Cake Addin
#addin nuget:?package=BlazorHooked&version=0.3.1

// Install BlazorHooked as a Cake Tool
#tool nuget:?package=BlazorHooked&version=0.3.1                

Get Started

Install from Nuget NuGet Status

Add the obligitory @@using BlazorHooked statement to _Imports.razor.

If you're intending on using a global store wrap your app in your App.razor.

@code {
    public record AppState();

    private Reducer<AppState> RootReducer(Reducer<AppState> reducer) => reducer;
}

<ConnectStore RootReducer=@RootReducer InitialStateFactory=@(() => new AppState())>
    <Router AppAssembly="@typeof(App).Assembly">
        <Found Context="routeData">
            <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
            <FocusOnNavigate RouteData="@routeData" Selector="h1" />
        </Found>
        <NotFound>
            <PageTitle>Not found</PageTitle>
            <LayoutView Layout="@typeof(MainLayout)">
                <p role="alert">Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</ConnectStore>

The HookContext

Hooks are accessed via a HookContext which you can get one of two ways.

Inherit from HookComponentBase in which case this.Hook exposes a single HookContext for the child component.

@inherits HookComponentBase

@{
    this.Hook.UseState(0);
}

Or use the Hook component, in which case the HookContext is scoped within the Hook. This gives more flexibility for you to inherit from other base components and even to create multiple contexts within a component.

<Hook>
    @{
        context.UseState(0);
    }
    <div>Hello</div>
</Hook>

You can rename the context to something more helpful and/or to avoid collisions.

<Hook Context="Hook">
    @{
        var (state, _) = Hook.UseState(0);
    }

    @state
</Hook>
<Hook Context="Hook2">
    @{
        var (state, _) = Hook2.UseState(1);
    }

    @state
</Hook>

You'll find there are very few classes or interfaces to inherit or implement in BlazorHooked. Actions and state in the examples are usually defined as records. The more you embrace immutibility the easier the Model View Update pattern becomes because you stop fighting the render loop and BlazorHooked is designed to foster that by using functional constructs wherever possible.

Read on to find out more about Hooks.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
0.3.2 489 2/16/2022
0.3.1 412 2/13/2022
0.3.0 396 2/12/2022
0.2.0 413 2/8/2022
0.1.6 427 2/2/2022
0.1.5 444 2/2/2022
0.1.4 432 2/2/2022
0.1.3 411 2/1/2022
0.1.2 416 2/1/2022
0.1.1 417 2/1/2022
0.1.0-alpha2 181 1/30/2022
0.1.0-alpha1 163 1/30/2022