Search code examples
javascriptc#.netrazorblazor

How to call blazor onclick method with static render mode


How to call onclick method while rendering component as a static html? I mean, is there any workarounds in JavaScript?

@(await Html.RenderComponentAsync<Awesome>(RenderMode.Static))

And the Awesome.razor

<h3>@name</h3>

<button @onclick="@(() => name = name.ToUpper())">Upper case me</button>

@code {
    string name = "Mr. Blazor";
}

While using ServerPrerendered it seems to work because oninitialize method is fire, but again onclick doesn't work.

@(await Html.RenderComponentAsync<Awesome>(RenderMode.ServerPrerendered))

Solution

  • Like the docs say:

    Static : Renders the component into static HTML

    That means an @onclick will never work, nor anything else in written in C#.

    is there any workarounds in JavaScript?

    JavaScript will still work, use onclick (without the @). But that won't let you use this name variable.