GithubHelp home page GithubHelp logo

ultimgos / blazor-hooked Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dibble-james/blazor-hooked

0.0 0.0 0.0 22.97 MB

A minimal boiler-plate, state management framework for Blazor that resembles React Hooks.

Home Page: https://dibble-james.github.io/blazor-hooked/

License: MIT License

C# 68.66% HTML 31.34%

blazor-hooked's Introduction

Blazor Hooked

Nuget

Get Started

Install from Nuget NuGet Badge

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.

blazor-hooked's People

Contributors

dibble-james avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.