Friday, November 28, 2025

thumbnail

Component-Based Development in Blazor

 Component-Based Development in Blazor


Blazor is a web UI framework from Microsoft that allows developers to build interactive web applications using C# instead of JavaScript. At the heart of Blazor is component-based development, a modern approach used by frameworks like React, Angular, and Vue.


What Is a Component in Blazor?


A Blazor component is a reusable piece of UI that can contain:


Markup (HTML)


C# logic


Event handlers


Styling (CSS)


Parameters for data input


Child components


Each component is typically defined in a .razor file.


Example component (Counter.razor):


<h3>Counter</h3>


<p>Current count: @count</p>


<button class="btn btn-primary" @onclick="IncrementCount">Increment</button>


@code {

    private int count = 0;


    private void IncrementCount()

    {

        count++;

    }

}


Key Features of Component-Based Development in Blazor

1. Reusability


Components can be reused across pages:


<Counter />

<Counter />



Each instance has its own state.


2. Component Parameters


Components accept data using parameters.


Parent (Parent.razor):


<Child Message="Hello from parent!" />



Child (Child.razor):


<p>@Message</p>


@code {

    [Parameter]

    public string Message { get; set; }

}


3. Event Callbacks


Components can notify parents of events.


Child:


<button @onclick="NotifyParent">Click Me</button>


@code {

    [Parameter]

    public EventCallback OnClicked { get; set; }


    private async Task NotifyParent()

    {

        await OnClicked.InvokeAsync();

    }

}



Parent:


<Child OnClicked="OnChildClicked" />


@code {

    private void OnChildClicked()

    {

        Console.WriteLine("Child was clicked!");

    }

}


4. Data Binding


Blazor supports one-way and two-way binding.


One-way binding:

<p>@value</p>


Two-way binding:

<input @bind="value" />


@code {

    private string value = "Hello";

}


5. Lifecycle Methods


Components have lifecycle events such as:


OnInitialized


OnParametersSet


OnAfterRender


Example:


@code {

    protected override void OnInitialized()

    {

        // Run logic when component initializes

    }

}


6. Nested Components


Components can contain other components, making UI maintainable and modular.


Example:


<Header />

<Sidebar />

<MainContent />

<Footer />


7. Routing


Blazor components can represent pages by adding a route:


@page "/about"


<h3>About Us</h3>


8. Dependency Injection in Components


Components can inject services:


@inject WeatherService WeatherService


<p>Temperature: @WeatherService.Temperature</p>


Benefits of Component-Based Development in Blazor

✔ Separation of concerns


UI and logic stay together in a component.


✔ Reusability


Write once, use anywhere.


✔ Testability


Components can be tested independently.


✔ Maintainability


Smaller components are easier to maintain and evolve.


✔ Consistency


Shared components unify design and behavior across the app.


When to Use Components in Blazor


Use a component when you need:


A reusable UI part (e.g., button, card, form input)


A part of UI that has its own logic or state


A section that may be used across multiple pages


A modular structure within a complex page

Learn Dot Net Course in Hyderabad

Read More

Introduction to TailwindCSS for Full Stack .NET Developers

Implementing Lazy Loading in Full Stack .NET Applications

How to Implement Real-Time Features with SignalR and React

Building Mobile Apps with Blazor WebAssembly

Visit Our Quality Thought Institute in Hyderabad

Get Directions 

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive