-
Notifications
You must be signed in to change notification settings - Fork 183
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
Blazor local functions in ChildContent don't work correctly #10236
Comments
@mkArtakMSFT @javiercn @SteveSandersonMS This seems to be a runtime issue. The output occurs during server side rendering, then disappears when the page updates its render mode. The code being generated by the compiler is: public partial class Home : global::Microsoft.AspNetCore.Components.ComponentBase
#nullable disable
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h1>Issue showcase</h1>\r\n\r\n");
__builder.OpenComponent<global::BlazorComponentsIssue.Client.Views.Components.IssueComponent>(1);
__builder.AddAttribute(2, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
__builder2.AddMarkupContent(3, "<span>Hello 1</span>");
}
));
__builder.CloseComponent();
__builder.AddMarkupContent(4, "\r\n\r\n");
__builder.OpenComponent<global::BlazorComponentsIssue.Client.Views.Components.IssueComponent>(5);
__builder.AddAttribute(6, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
#nullable restore
#line (10,7)-(12,5) "C:\Projects\scratch\BlazorComponentsIssue\BlazorComponentsIssue\BlazorComponentsIssue.Client\Views\Pages\Home.razor"
IssueLocalFunction();
#line default
#line hidden
#nullable disable
}
));
__builder.CloseComponent();
#nullable restore
#line (15,3)-(18,1) "C:\Projects\scratch\BlazorComponentsIssue\BlazorComponentsIssue\BlazorComponentsIssue.Client\Views\Pages\Home.razor"
void IssueLocalFunction()
{
#line default
#line hidden
#nullable disable
__builder.AddMarkupContent(7, "<span>Hello 2</span>");
#nullable restore
#line (19,1)-(20,1) "C:\Projects\scratch\BlazorComponentsIssue\BlazorComponentsIssue\BlazorComponentsIssue.Client\Views\Pages\Home.razor"
}
#line default
#line hidden
#nullable disable Which seems plausibly correct, but I guess the local function call is throwing something off. Does the builder get replaced, meaning the one captured in the closure of the local function would now be wrong? |
@chsienki I think the problem is that the local function uses |
Yes, that's exactly the problem. To be honest this syntax should not exist: void IssueLocalFunction()
{
<span>Hello 2</span>
} Any syntax like that is very dangerous as it's capturing the We never added any syntax like that on purpose, but it happened on its own when C# added local functions in general. As far as I understand, Razor inherited the feature by default even though it's actively harmful in this case. If it's somehow possible to prevent local functions in |
When I call local functions in @{} block inside a .razor component tag the content from the local function isn't getting rendered.
Home.razor
IssueComponent.razor
Output
It's getting showed when is prerendering but then it disappears.
Here is my repo with that issue:
https://github.com/EngieDev/BlazorComponentsIssue
The text was updated successfully, but these errors were encountered: