Skip to content
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

Why @ref or ElementReference always has a value after first set value into it and when it does not rendered throw an exception? #56250

Closed
1 task done
NaserParhizkar opened this issue Jun 15, 2024 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@NaserParhizkar
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

As you can see in the following code @ref here which is a InputText component after first render always has value even when I don't want render it in the page

<div>
    <input type="button" style="margin:5px;" @onclick="@(() => flag = false)" value="Hide Input" />
    <input type="button" style="margin:5px;" @onclick="@(() => flag = true)" value="Show Input" />
</div>

<div>
    <input type="button" style="margin:5px;" @onclick="focus" value="focus" />
    @if (flag)
    {
        <InputText @ref="inTextElement" @bind-Value="textValue" />
    }
</div>

@code {
    InputText inTextElement;
    string textValue;
    bool flag = true;

    async Task focus()
    {
        if (inTextElement.Element.HasValue)
        {
            await inTextElement.Element.Value.FocusAsync();
        }
    }
}

Expected Behavior

I expect Element.HasValue will be false when it does not rendered in the page

   async Task focus()
   {
       if (inTextElement.Element.HasValue)
       {
           await inTextElement.Element.Value.FocusAsync();
       }
   }

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8.0.0.2

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jun 15, 2024
@NaserParhizkar NaserParhizkar changed the title Why @ref or ElementReference always has a value after first set value into it? Why @ref or ElementReference always has a value after first set value into it and when it does not rendered in the page when I check Element.Value it it true? Jun 15, 2024
@NaserParhizkar NaserParhizkar changed the title Why @ref or ElementReference always has a value after first set value into it and when it does not rendered in the page when I check Element.Value it it true? Why @ref or ElementReference always has a value after first set value into it and when it does not rendered throw a exception? Jun 15, 2024
@NaserParhizkar NaserParhizkar changed the title Why @ref or ElementReference always has a value after first set value into it and when it does not rendered throw a exception? Why @ref or ElementReference always has a value after first set value into it and when it does not rendered throw an exception? Jun 15, 2024
@javiercn
Copy link
Member

@NaserParhizkar thanks for contacting us.

Element references are values and we only set them when the element renders. If the element doesn't render, we don't set it to a default value again.

That's something your app needs to do if its conditionally rendering the element, otherwise we would need to reset all the elements on every render which would incur on a perf-hit.

@javiercn javiercn added question ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. labels Jun 15, 2024
Copy link
Contributor

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@NaserParhizkar
Copy link
Author

I think prevent to raise an exception is better than to have much attention on perf-hit and consequently loose the reliability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants