In my last article, we explored the fundamentals of components in Blazor and how they contribute to creating a Single Page Application (SPA). You can read it here. But what is this entity called a component?
let me give you a 50,000-foot view of what a component in Blazor is.
A component is a reusable piece of UI; it is a combination of C# code, HTML markup, and CSS styles. We use components to render UI, encapsulate UI logic with data, and handle user interactions. Components can interact with each other through parameters and events, and they automatically update the UI when their state changes.
This is the basic syntax of a component.
@page "/mycomponent"
<h3>Hello, @name!</h3>
<button @onclick="ChangeName">Change Name</button>
@code {
private string name = "Blazor";
private void ChangeName()
{
name = "World";
}
}
Code Snippet 1. Basic Component Syntax
Types of Components in Blazor
In this article, we'll explore four types of components.
- Routable Components
- Non-Routable Components
- Templated Components
- Dynamic Components
To illustrate these components, we'll create a Marvel superhero app. Start by creating a Blazor Web App using any template you prefer.
1. Routable Components
A routable component is accessed via a URL. These components define routes using the @page directive, which specifies the URL that maps to the component. Routable components typically go in the Pages folder.
@page "/superheroes"
@using Models
@using MyFirstBlazorWebApp.Components.UI;
<h3>Marvel Superheroes</h3>
<div>
@foreach (var hero in superheroes)
{
<div>
<SuperHero Hero="hero" />
</div>
}
</div>
@code {
private List<Superhero> superheroes = new List<Superhero>
{
new Superhero { Name = "Spider-Man", Superpower = "Wall-crawling, web-shooting, spider-sense" },
new Superhero { Name = "Iron Man", Superpower = "Genius-level intellect, powered armor suit" },
new Superhero { Name = "Captain America", Superpower = "Superhuman strength, agility, endurance" },
new Superhero { Name = "Thor", Superpower = "God of Thunder, superhuman strength, control over lightning" },
new Superhero { Name = "Hulk", Superpower = "Unlimited strength, invulnerability" }
};
}
Code snippet 2. SuperHeroes.razor
In this example, this component lists various superheroes and uses another component called SuperHero.razor, at line number 22, to display details for each hero. Users can navigate to "/superheroes" to see this component.
2. Non-Routable Components
Non-routable components are components that cannot be directly accessed via a URL. These components are typically reusable UI elements that can be used within other components. Non-routable components usually go in a Components folder or a specific folder like Components/UI.
In the above example, the one we are calling at line number 22 is a non-routable component SuperHero.razor, which you can see below. It can be reused inside any component.
@using MyFirstBlazorWebApp.Models
<div>
<div>
<h5>@Hero.Name</h5>
<p><strong>Superpowers:</strong> @Hero.Superpower</p>
</div>
</div>
@code {
[Parameter] public Superhero Hero { get; set; }
}
Code snippet 3. SuperHero.razor
It is used within the SuperHeroes component to display individual SuperHero details. It accepts a parameter, Hero, which contains the superhero's information.
Let's see how it looks in the output. In the following image 1, I am loading the routable component "SuperHeroes.razor" through the URL "/superheroes", which then goes through a list of movies and loads the non-routable component "SuperHero.razor" for each movie.

Image 1: Routable component loading a non-routable component
3. Templated Components
Templated components allow you to define a UI template for their content. They use RenderFragment<T> to create a template for items in a collection.
Let's create a MovieTemplate component.
To define a template, we need the following.
- @typeparam TItem: This defines a generic type parameter TItem for the component, allowing it to be used with any type.
- Items parameter: Accepts a collection of items of type TItem to be rendered by the component.
- ItemTemplate parameter: Accepts a render fragment that defines how each item should be rendered. The RenderFragment<TItem> type allows the template to take an item of type TItem as a parameter.
@typeparam TItem
<div>
@foreach (var item in Items)
{
@ItemTemplate(item)
}
</div>
@code {
[Parameter] public IEnumerable<TItem> Items { get; set; }
[Parameter] public RenderFragment<TItem> ItemTemplate { get; set; }
}
Code snippet 4. MovieTemplate.razor
Now let's use the MovieTemplate component in the following Movies.razor component.
We're calling MovieTemplate at line number 8 in code snippet 5, where we're passing a list of movies to the Items parameter in MovieTemplate. Immediately following Items, we pass ItemTemplate, which is then forwarded to the RenderFragment of MovieTemplate to define how each movie item should be rendered.




Join the conversation! Your thoughts help the community grow.