question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exposing the inner ref of an element via BlazorComponent returns an emtpy(?) ref

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
SimantoRcommented, Jul 3, 2019

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

0reactions
SteveSandersonMScommented, Mar 5, 2019

@AndreasTruetschel is correct. You will need bind-something for this to work, otherwise this line of code:

builder.AddElementReferenceCapture(2, capturedRef => { ElementRef = capturedRef; });

… 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found