Force reload blazor component
In this video we will discuss how to force reload blazor component. When a blazor component is used for multiple workflows, this feature allows us to refresh the component, so the user can see the elements that are changed dynamically.
Uisng a blazor component for multiple workflows
For example, EditEmployee
component is used for the following 2 workflows
- Editing existing employee data
- Creating a new employee
In the following example, PageHeader
is set to Create Employee
if EmployeeId is 0
, otherwise Edit Employee
The view binds to the PageHeader
property
Why force-reload blazor component
If your navigation flow is the following, then the PageHeader
text changes from Edit Employee
to Create Employee
as expected.
- Navigate to EditEmployee Component (for editing existing employee data).
- Navigate to a different component, for example ListEmployees component.
- Navigate to EditEmployee Component (for creating a new employee).
However, if your navigation flow is the following, then the PageHeader
text is not refreshed.
- Navigate to EditEmployee Component (for editing existing employee data)
- Navigate to EditEmployee Component (for creating a new employee).
Force reload blazor nav link
When you are already on EditEmployee
component, and when the <NavLink>
element is clicked, it does not send the request to the server, the redirection happens on the client. Hence, nothing on the page updates.
To fix this
- Include
onclick
event handler NavigationManager.NavigateTo()
method has a booleanforceLoad
parameter.- The default value is
false
. Passtrue
to this parameter. - This will by-pass client-side routing and forces the browser to load the component from the server.
Blazor navigation menu selected style
The following are the 2 routes to get to EditEmployee
component. When a value for employee id
is passed in the URL, the component is used for editing
else for creating a new employee
.
The following are the 2 navigation menu items
The URL (/editemployee/1)
matches both the navigation menu items (Create & Edit)
and as a result, selected style is applied to both.
To fix this set Match="NavLinkMatch.All"
on both the menu items
NavLinkMatch.All
, specifies that the selected style must be applied to theNavLink
when it matches the entire current URL.NavLinkMatch.Prefix
specifies that the selected style must be applied to theNavLink
when it matches any prefix of the current URL.
© 2020 Pragimtech. All Rights Reserved.