Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OnMounted component lifecycle event #49497

Closed
1 task done
marinasundstrom opened this issue Jul 18, 2023 · 3 comments
Closed
1 task done

Add OnMounted component lifecycle event #49497

marinasundstrom opened this issue Jul 18, 2023 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue.

Comments

@marinasundstrom
Copy link

marinasundstrom commented Jul 18, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I want to invoke JavaScript after a pre-rendered server component has been mounted in the browser. But I can't find a way to do it.

It used to be (with Razor Pages) that you did it in OnAfterRender which ran twice, and you checked firstRender. For Blazor Server, that meant that if it was true then you where running in the browser and was able to invoke JavaScript. While for WebAssembly you had to check for it to be false.

In Blazor Server in .NET 8, it doesn't work that way.

And this behavior is reasonable. It makes sense that when you have pre-rendered a server component you have already rendered it, and there is no need to call the lifecycle event method again.

To me it makes the most sense to add a new event method indicating that the code has been materialized or "mounted" in the browser.

Describe the solution you'd like

Add an OnMounted() lifecycle method that is executed after OnAfterRender has been executed the first time and the component has been added to the Browsers DOM and the SignalR connection is up and running

This will enable the to execute logic when the component has been mounted - like invoking JavaScript,

@inject IJSRuntime JS

<div id="obj">Hello</div>

@code 
{
    protected async Task OnMountedAsync()
    {
        // The component has been mounted in the DOM and is ready for interactivity.

        await JS.InvokeVoidAsync("hello");
    }
}

Additional context

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jul 18, 2023
@marinasundstrom
Copy link
Author

Perhaps related to this: #26794

@mkArtakMSFT
Copy link
Member

In Blazor Server in .NET 8, it doesn't work that way.

Thanks for contacting us. It's not clear why OnAfterRender doesn't work for you. It should, as the event fires when the component is already rendered, and the interactivity is enabled. Can you please share more details about the "non-working" experience that you're facing.

@mkArtakMSFT mkArtakMSFT added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Jul 19, 2023
@marinasundstrom
Copy link
Author

@mkArtakMSFT I checked it out and you are right. It does work. I had just been playing with stuff over the course of a couple of previews so it was broken.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue.
Projects
None yet
Development

No branches or pull requests

2 participants