liveBook · Manning

Lifecycle

As explained in Pro ASP.NET Core 7, Tenth Edition

A component lifecycle is the sequence of stages and events governing a component's initialization, parameter assignment, rendering, updating, and disposal.

A Blazor component lifecycle is the sequence of framework callbacks around initialization, parameter assignment, rendering, updates, and disposal. Override lifecycle methods in the component’s @code block to respond at the appropriate stage. section 35.0 section 35.3

Use the lifecycle callbacks for these transitions:

  • OnInitialized or OnInitializedAsync runs when the component is first initialized and can establish initial state. section 35.3
  • OnParametersSet or OnParametersSetAsync runs after [Parameter] properties receive their values, making it suitable for parameter-dependent setup. section 35.3
  • ShouldRender runs before an update render; returning false suppresses that update but not the component’s initial rendering. section 35.3
  • OnAfterRender or OnAfterRenderAsync runs after content is rendered, with a Boolean indicating the initial render. section 35.3
  • Implement IDisposable when routed components need cleanup after URL changes remove them from the display. section 35.3

Lifecycle timing matters when routing or parameter changes drive component behavior. A component can wait for assigned parameters before extracting values, subscribe to NavigationManager.LocationChanged, and recalculate its rendered state as the URL changes. section 35.3

Load parameter-dependent data asynchronously

A PersonDisplay component receives an identifier through a route parameter and queries the database in OnParametersSetAsync. Person remains null while the task runs, so the markup displays “Loading...”. Blazor renders the initial content without waiting, then automatically rerenders the component when the query assigns a result or a fallback Person object. section 35.3.1

Table 35.5 The Razor Component lifecycle methods (view table figure)

Name

Description

OnInitialized()

OnInitializedAsync()

These methods are invoked when the component is first initialized.

OnParametersSet()

OnParametersSetAsync()

These methods are invoked after the values for properties decorated with the Parameter attribute have been applied.

ShouldRender()

This method is called before the component’s content is rendered to update the content presented to the user. If the method returns false, the component’s content will not be rendered, and the update is suppressed. This method does not suppress the initial rendering for the component.

OnAfterRender(first)

OnAfterRenderAsync(first)

This method is invoked after the component’s content is rendered. The bool parameter is true when Blazor performs the initial render for the component.

Use the lifecycle-method table as a quick reference for callback timing, parameters, render suppression, and post-render notification. section 35.3

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Defines callback timing and demonstrates synchronous setup, URL-change handling, disposal, render suppression, and asynchronous loading behavior.
Examples
Uses MultiNavLink for URL-sensitive navigation and PersonDisplay for parameterized asynchronous database loading.
What it assumes
Assumes Razor Components, routes, parameters, and layouts; teaches lifecycle callback timing and asynchronous rendering behavior.
What you can do after
Apply it via lifecycle overrides to initialize components, react to parameters, control updates, load data, and clean up routed components.
How it connects
Connects lifecycle methods to URL routing, component parameters, rendering, NavigationManager, IDisposable, layouts, and database queries.

Also explained in

Compare how 6 books explain lifecycle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.