-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Blazor change detection fires for a property that does not change #11248
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
Comments
In the doc we can read
It looks that it works as designed and documented. You can add a private bool variable private bool firstParametersSet = true;
protected override void OnParametersSet()
{
if (firstParametersSet)
{
firstParametersSet = false;
Console.WriteLine("OnParameterSet invoked.");
currentCount = Initial;
}
} |
@Andrzej-W, I know that your suggestion is a good work-around, and I'm also aware of the fact you have cited from the documentation :-). In the sample I attached, it is not clear why Blazor re-renders the Do you know why it happens this way? |
Sorry @Dotneteer - now I think that I understand what you are asking for. Blazor re-renders component after each event. Here On the other hand, when you handle Now I think that Blazor works as expected, but not as documented. This doc:
should be changed to something like this:
I will open a new issue in https://github.com/aspnet/AspNetCore.Docs |
@Andrzej-W, thanks for your explanation, it sounds absolutely logical. According to it, I would not call this phenomenon a bug, rather sub-optimal behavior. I guess, before re-rendering the parent, it can be checked if there's real change (and not just potential) in the state. Probably this check is cheaper than re-rendering :-). |
This is not necessarily true. You can pass complex objects as parameters, for example |
Agree with you, this check can be very expensive in many cases, especially for complex object graphs. Nonetheless, there are very simple cases, especially when you need to compare intrinsic .NET value types. I can accept Blazor works as it is, however, I'm happy to mention where I see some opportunity for performance optimization :-). Thanks for your explanation and reporting the documentation issue! |
Thanks for contacting us, @Dotneteer. If this comes up in our performance testing (#10449) we'll look into it as needed. |
Bug description:
In the following example (created from the default Blazor project), the
Index
component passes an input parameter,Initial
, toCounter
, and gets a notification whenever the counter value changes:Counter
sets its starting value in theOnParametersSet
lifecycle method. Whenever the counter's value changes, it sends a notification:Every time
Counter
raises theChanged
event, Blazor invokes theOnParametersSet()
method, though the singleInitial
input parameter ofCounter
does not change at all. IfChanged
is not raised orIndex
does not handle this event callback, everything works fine.To Reproduce
Steps to reproduce the behavior:
Expected behavior
I would expect this output (
OnParametersSet
invoked only once, asInitial
does not change):WASM: OnParameterSet invoked.
WASM: Value: 8, Initial: 7
WASM: Value: 9, Initial: 7
WASM: Value: 10, Initial: 7
The text was updated successfully, but these errors were encountered: