Blazor Components in Blazor
Blazor Components is the second chapter of the free Blazor tutorial. It shows how to build your own components and how to use the ones Blazor gives you.
What each lesson covers:
- Components Basics: What a component is and how it renders, creating and using components, parent and child components, parameters, required and cascading parameters, child content, EventCallback and CSS isolation.
- Built-in Components: Router and @page routes, query strings and route patterns, the Not Found page, NavLink, FocusOnNavigate, PageTitle, HeadContent, Virtualize and ErrorBoundary.
- Component Lifecycle: The order of lifecycle methods, loading data, parameter changes, StateHasChanged, ShouldRender, @key, OnAfterRender and Dispose.
- Layouts: LayoutComponentBase and @Body, default, page and folder layouts, nested layouts, LayoutView, NavMenu, sections and the layout error UI.
Every code box compiles your components with the real Razor compiler and runs them on .NET 10 in your browser.
Lessons in this section
- Components Basics
What a Blazor component is and how it renders, creating and using components, parent and child components, [Parameter], [EditorRequired], cascading values, ChildContent, EventCallback and CSS isolation.
- Create a component and use it in a page
- Pass data down with [Parameter] and [CascadingParameter]
- Accept child content with RenderFragment
- Send events up with EventCallback
- Built-in Components
Blazor's ready-made components: Router and @page routes, query strings and route patterns, the Not Found page, NavLink, FocusOnNavigate, PageTitle, HeadContent, Virtualize with ItemsProvider and ErrorBoundary.
- Explain how the Router matches @page routes and reads query strings
- Highlight the open page with NavLink
- Set the tab title with PageTitle and HeadContent
- Show long lists fast with Virtualize
- Component Lifecycle
The order of Blazor's lifecycle methods: OnInitialized(Async), SetParametersAsync, OnParametersSet(Async), route parameter changes, re-render triggers, StateHasChanged, ShouldRender, @key, OnAfterRender with firstRender, and Dispose.
- Name the lifecycle methods in order
- Load data in OnInitializedAsync and react to parameters in OnParametersSet
- Update the page with StateHasChanged and keep list items with @key
- Run one-time setup with firstRender and clean up in Dispose
- Layouts
Shared page frames in Blazor: LayoutComponentBase and @Body, the default layout, nested layouts, LayoutView, the NavMenu, sections with SectionOutlet and SectionContent, and the layout's error UI.
- Write a layout with LayoutComponentBase and @Body
- Set the main layout with DefaultLayout in Routes.razor
- Nest layouts and use LayoutView
- Fill a layout spot with SectionOutlet and SectionContent