Skip to content

Blazor Component not updating with StateHasChanged after login #26400

@svelluto

Description

@svelluto

Describe the bug

I have a layout with a component responsible for displaying a modal of another component. After I go through the login and it redirects to a page the component in the modal does not appear even though the method calling StateHasChanged does. If I then refresh the page, which uses the authentication cookie instead of typing a password, the modal works correctly.

To Reproduce

I'm using Auth0 for authentication. It's the standard setup.

The Component for my Modal:
<RightSideModal @ref="CurrentSession.RightModal" />

Simplified version of component razor file:

@if (BodyControl != null)
{
    <div id="rightSideModal">
        <div>@Header</div>
        <div>@BodyControl</div>
    </div>
}

The method, in the RightSideModalModel class, that handles displaying the modal:

public async Task ShowModal<T>(Dictionary<String, Object> attributes = null, String header = "") where T : IComponent
{
    Header = header;

    BodyControl = builder =>
    {
        builder.OpenComponent<T>(0);

        if (attributes != null)
        {
            foreach (var xItem in attributes)
            {
                builder.AddAttribute(1, xItem.Key, xItem.Value);
            }
        }

        builder.CloseComponent();
    };

    StateHasChanged();

    await _jsRuntime.InvokeVoidAsync("showModalTrigger", "#rightSideModal");
           
}

When I type in a password the login redirects to the home page, I have a button that calls ShowModal. It calls the method, the BodyControl property gets set (not null), StateHasChanged() gets called and nothing happens. I put breakpoints through the method and the razor file, the ones in the method got called, but the ones in the razor file did not.

If I have auth cookies and just refresh, the page goes through login and redirects to the home page, everything works and all breakpoints are triggered.

Exceptions (if any)

There are no errors

Further technical details

  • Blazor Server
  • NetCore 3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.Status: No Recent Activityarea-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions