Blazor Lifecycle Flashcards

(14 cards)

1
Q

What is the first method called when the component is instantiated or receives updated parameters from its parent?

A

SetParametersAsync(ParameterView parameters)
This method is used to set default parameter values or perform actions based on initial parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What method must be called to ensure subsequent lifecycle methods are triggered correctly after SetParametersAsync?

A

await base.SetParametersAsync(parameters)
This ensures that the component lifecycle continues as expected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of the OnInitialized() / OnInitializedAsync() method?

A

Perform one-time initialization tasks
This includes tasks like fetching data from an API or setting default values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In Blazor Server apps with prerendering enabled, how many times is OnInitializedAsync called?

A

Twice
Once during the initial static render and again when the client connection is established.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of the OnParametersSet() / OnParametersSetAsync() method?

A

Run operations every time parameters change
This is useful for recomputing values based on new parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the .ShouldRender() method control?

A

Whether the UI should update
By default, it returns true, but can be overridden for performance optimization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if .ShouldRender() returns false?

A

Prevents the component from re-rendering
The component will not update unless StateHasChanged() is called explicitly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of the OnAfterRender(bool firstRender) / OnAfterRenderAsync(bool firstRender) method?

A

Called after the component has finished rendering
This is used for JavaScript interop calls or interacting with third-party libraries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the firstRender parameter indicate in the OnAfterRender method?

A

True for the first render, false for subsequent renders
This helps differentiate between the initial render and re-renders.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of the .StateHasChanged() method?

A

Notifies the component that its state has changed
This triggers a re-render when necessary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When should .Dispose() / .DisposeAsync() be called?

A

When the component is removed from the UI
This is for cleaning up unmanaged resources and preventing memory leaks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What interface must the component implement for Dispose methods to be called automatically?

A

IDisposable or IAsyncDisposable
This ensures proper resource management when the component is disposed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what life cycle part to use when the dom is created?

A

To interact with the DOM after it has been created and rendered, you should use the OnAfterRender or OnAfterRenderAsync Blazor lifecycle methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what life cycle part to use before dom created?

A

To perform actions before the DOM (Document Object Model) is created in a Blazor component, you should use the OnInitialized{Async} or SetParametersAsync lifecycle methods. The DOM is only updated after these initialization phases are complete and the component has been rendered to an in-memory render tree.

The OnAfterRender{Async} methods are the first point where you can safely interact with the rendered DOM elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly