Exposing the inner ref of an element via BlazorComponent returns an emtpy(?) ref
See original GitHub issueI have a component that inherits from BlazorComponent with this code:
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
base.BuildRenderTree(builder);
builder.OpenElement(0, TagName);
foreach (var param in _attributesToRender)
{
builder.AddAttribute(1, param.Key, param.Value);
}
if (_classname != null) builder.AddAttribute(1, "class", _classname);
builder.AddElementReferenceCapture(2, capturedRef => { ElementRef = capturedRef; });
builder.AddContent(3, _childContent);
builder.CloseElement();
}
It’s used like so:
<form onSubmit={this.alertValue}>
<Dynamic TagName="input" ElementRef="@inputRef" />
<button type="button" onclick="@OnClick">
Submit
</button>
</form>
@functions {
ElementRef inputRef;
async void OnClick()
{
await inputRef.AlertValue();
}
}
Which ultimately prints out to the console what the ref is. Like so:
public static class ElementRefExtensions
{
public static async Task AlertValue(this ElementRef elementRef)
{
await JSRuntime.Current.InvokeAsync<bool>("blazorousSample.alertValue", elementRef);
}
}
window.blazorousSample = {
alertValue: function (element) {
console.log("element: %O", element);
alert(element.value);
}
};
One the javascript console I get:
{
"_blazorElementRef": null
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Exposing the inner ref of an element via BlazorComponent ...
I have a component that inherits from BlazorComponent with this code: protected override void BuildRenderTree(RenderTreeBuilder builder) ...
Read more >Blazor - How to manager @ref for initial invisible part of a ...
1 Answer 1 · You can't actually be sure that MyRef will be set during the Yield() - it depends on how it's...
Read more >Handle errors in ASP.NET Core Blazor apps
This article describes how Blazor manages unhandled exceptions and how to develop apps that detect and handle errors.
Read more >ASP.NET Core Blazor data binding
This article explains data binding features for Razor components and DOM elements in Blazor apps. Razor components provide data binding ...
Read more >Component inside tab with @ref results in null in UI for Blazor
I have a child component that I would like to access from parent so i put @ref="myChild", when this control is inside a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I believe this should be added to the official Input[*] components that are meant to be used with EditForm. I recently wanted to build a word processor for a complicated form and took me a whole day to find and form a working solution
@AndreasTruetschel is correct. You will need
bind-something
for this to work, otherwise this line of code:… is only writing to its own
ElementRef
property, not to the corresponding property on the caller.Since you’re writing your component in raw C# instead of Razor, you’ll have to figure out what C# code is equivalent to the
bind-something
that Razor would generate.