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





