Skip to content

Commit c46bcf2

Browse files
authored
Blazor lifecycle diagram and coverage (#19543)
1 parent b56dad4 commit c46bcf2

7 files changed

Lines changed: 34 additions & 4 deletions

File tree

aspnetcore/blazor/components/lifecycle.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to use Razor component lifecycle methods in ASP.NET Core
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 07/06/2020
8+
ms.date: 10/06/2020
99
no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: blazor/components/lifecycle
1111
---
@@ -15,6 +15,36 @@ By [Luke Latham](https://github.com/guardrex) and [Daniel Roth](https://github.c
1515

1616
The Blazor framework includes synchronous and asynchronous lifecycle methods. Override lifecycle methods to perform additional operations on components during component initialization and rendering.
1717

18+
The following diagrams illustrate the Blazor lifecycle. Lifecycle methods are defined with examples in the following sections of this article.
19+
20+
Component lifecycle events:
21+
22+
1. If the component is rendering for the first time on a request:
23+
* Create the component's instance.
24+
* Perform property injection. Run [`SetParametersAsync`](#before-parameters-are-set).
25+
* Call [`OnInitialized{Async}`](#component-initialization-methods). If a <xref:System.Threading.Tasks.Task> is returned, the <xref:System.Threading.Tasks.Task> is awaited and then the component is rendered. If a <xref:System.Threading.Tasks.Task> isn't returned, render the component.
26+
1. Call [`OnParametersSet{Async}`](#after-parameters-are-set). If a <xref:System.Threading.Tasks.Task> is returned, the <xref:System.Threading.Tasks.Task> is awaited and then the component is rendered. If a <xref:System.Threading.Tasks.Task> isn't returned, render the component.
27+
28+
![Component lifecycle events of a Razor component in Blazor](lifecycle/_static/lifecycle1.png)
29+
30+
Document Object Model (DOM) event processing:
31+
32+
1. The event handler is run.
33+
1. If a <xref:System.Threading.Tasks.Task> is returned, the <xref:System.Threading.Tasks.Task> is awaited and then the component is rendered. If a <xref:System.Threading.Tasks.Task> isn't returned, the component is rendered.
34+
35+
![Document Object Model (DOM) event processing](lifecycle/_static/lifecycle2.png)
36+
37+
The `Render` lifecycle:
38+
39+
1. If this isn't the component's first render or [`ShouldRender`](#suppress-ui-refreshing) is evaluated as `false`, don't perform further operations on the component.
40+
1. Build the render tree diff (difference) and render the component.
41+
1. Await the DOM to update.
42+
1. Call [`OnAfterRender{Async}`](#after-component-render).
43+
44+
![Render lifecycle](lifecycle/_static/lifecycle3.png)
45+
46+
Developer calls to [`StateHasChanged`](#state-changes) result in a render.
47+
1848
## Lifecycle methods
1949

2050
### Before parameters are set
@@ -175,7 +205,7 @@ In the `FetchData` component of the Blazor templates, <xref:Microsoft.AspNetCore
175205

176206
`Pages/FetchData.razor` in the Blazor Server template:
177207

178-
[!code-razor[](lifecycle/samples_snapshot/3.x/FetchData.razor?highlight=9,21,25)]
208+
[!code-razor[](lifecycle/samples_snapshot/FetchData.razor?highlight=9,21,25)]
179209

180210
## Handle errors
181211

@@ -270,11 +300,11 @@ Unsubscribe event handlers from .NET events. The following [Blazor form](xref:bl
270300

271301
* Private field and lambda approach
272302

273-
[!code-razor[](lifecycle/samples_snapshot/3.x/event-handler-disposal-1.razor?highlight=23,28)]
303+
[!code-razor[](lifecycle/samples_snapshot/event-handler-disposal-1.razor?highlight=23,28)]
274304

275305
* Private method approach
276306

277-
[!code-razor[](lifecycle/samples_snapshot/3.x/event-handler-disposal-2.razor?highlight=16,26)]
307+
[!code-razor[](lifecycle/samples_snapshot/event-handler-disposal-2.razor?highlight=16,26)]
278308

279309
## Cancelable background work
280310

67.5 KB
Loading
38.9 KB
Loading
83.1 KB
Loading

aspnetcore/blazor/components/lifecycle/samples_snapshot/3.x/FetchData.razor renamed to aspnetcore/blazor/components/lifecycle/samples_snapshot/FetchData.razor

File renamed without changes.

aspnetcore/blazor/components/lifecycle/samples_snapshot/3.x/event-handler-disposal-1.razor renamed to aspnetcore/blazor/components/lifecycle/samples_snapshot/event-handler-disposal-1.razor

File renamed without changes.

aspnetcore/blazor/components/lifecycle/samples_snapshot/3.x/event-handler-disposal-2.razor renamed to aspnetcore/blazor/components/lifecycle/samples_snapshot/event-handler-disposal-2.razor

File renamed without changes.

0 commit comments

Comments
 (0)