Skip to content

Js initialization in OnAfterRender stopped working with 0.7.0 #5649

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

Closed
fs86 opened this issue Nov 19, 2018 · 6 comments
Closed

Js initialization in OnAfterRender stopped working with 0.7.0 #5649

fs86 opened this issue Nov 19, 2018 · 6 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components investigate

Comments

@fs86
Copy link

fs86 commented Nov 19, 2018

After I upgraded to Blazor 0.7.0 I noticed that initialization scripts of 3rd party JavaScript libraries which I usually call in OnAfterRender stopped working.
E.g. I use select2 (https://github.com/select2/select2) to apply a custom style for my select fields. But it seems like something has changed with Blazor 0.7.0, because the following code (which used to work in 0.6.0) dosn't work anymore:

protected override void OnAfterRender()
{
  base.OnAfterRender();
  JsInterop.InitializeSelect2();
}

My JavaScript function:

window.JsFunctions = {
  initializeSelect2: function() {
    $('select').select2();
  }
}

The control style will get applied when I call StateHasChanged() after JsInterop.InitializeSelect2(), but this will end up in an endless loop. Is there a better place to call InitializeSelect2() than OnAfterRender?

Edit:
I found the following statement about lifecycle methods in the official documentation:

OnAfterRenderAsync and OnAfterRender are called each time after a component has finished rendering. Element and component references are populated at this point. Use this stage to perform additional initialization steps using the rendered content, such as activating third-party JavaScript libraries that operate on the rendered DOM elements.

After doing a bit of research I can say that the corresponding field is not accessible in OnAfterRender.

@grahamehorner
Copy link
Contributor

@fs86 I have also seen this issue when using 0.7.0 where the overridden OnAfterRender is not getting fired,

@grahamehorner
Copy link
Contributor

@fs86 looks like there are is a lot of refactoring happening at present, check out
https://github.com/aspnet/Blazor/blob/master/test/testapps/BasicTestApp/InteropComponent.cshtml

@fs86
Copy link
Author

fs86 commented Nov 23, 2018

@fs86 I have also seen this issue when using 0.7.0 where the overridden OnAfterRender is not getting fired,

It is getting fired but DOM elements are not accessible in OnAfterRender. I think it might be possible that the event is getting fired too early.

@aweFalafelApps
Copy link

Anyone happen to have a workaround for this while we wait for it to hopefully be fixed in 0.8.0?

@aspnet-hello aspnet-hello transferred this issue from dotnet/blazor Dec 17, 2018
@aspnet-hello aspnet-hello added this to the 0.8.0 milestone Dec 17, 2018
@aspnet-hello aspnet-hello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-blazor Includes: Blazor, Razor Components investigate labels Dec 17, 2018
@mkArtakMSFT mkArtakMSFT modified the milestones: 0.8.0, 3.0.0-preview3 Jan 9, 2019
@Lupusa87
Copy link
Contributor

I am not sure this will be workaround for this case, but you can try.

Please follow timer2 in code

Idea here is to delay some method call.

In my case I used this trick because calling my method inside OnAfterRender was doing nothing, but from timer callback it is working as expected.

Hope it helps.

@mkArtakMSFT mkArtakMSFT removed this from the 3.0.0-preview3 milestone Feb 22, 2019
@mkArtakMSFT
Copy link
Member

Closing this as this is definitely working now. Please check out a recent release and if you would experience the same issue feel free to file a new issue.

@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components investigate
Projects
None yet
Development

No branches or pull requests

7 participants