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.

MouseEvent causes re-render on every click?

See original GitHub issue

Describe the bug

Clicking on a component causes a re-render of that component.

Example, if I click anywhere within a component on a page, my debug log begins with this:

[15:47:36 0HLS5HMITRJSV:00000001 DBG] <Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher> Received hub invocation: InvocationMessage { InvocationId: "", Target: "DispatchBrowserEvent", Arguments: [ {"browserRendererId":0,"eventHandlerId":12,"eventArgsType":"mouse","eventFieldInfo":null}, {"type":"click","detail":1,"screenX":2343,"screenY":281,"clientX":423,"clientY":178,"button":0,"buttons":0,"ctrlKey":false,"shiftKey":false,"altKey":false,"metaKey":false} ], StreamIds: [  ] }.
[15:47:36 0HLS5HMITRJSV:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Handling event {"Id": 5, "Name": "HandlingEvent"} of type 'MouseEventArgs'
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 16 of type GridBlazor.Pages.GridComponent`1[Model.EventNotification]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 17 of type Microsoft.AspNetCore.Components.CascadingValue`1[GridBlazor.Pages.GridComponent`1[Model.EventNotification]]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 29 of type GridBlazor.Pages.GridExtSortHeaderComponent`1[Model.EventNotification]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 18 of type Microsoft.AspNetCore.Components.CascadingValue`1[GridBlazor.Pages.GridComponent`1[Model.EventNotification]]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 30 of type GridBlazor.Pages.GridHeaderComponent`1[Model.EventNotification]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 19 of type Microsoft.AspNetCore.Components.CascadingValue`1[GridBlazor.Pages.GridComponent`1[Model.EventNotification]]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 31 of type GridBlazor.Pages.GridHeaderComponent`1[Model.EventNotification]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 20 of type Microsoft.AspNetCore.Components.CascadingValue`1[GridBlazor.Pages.GridComponent`1[Model.EventNotification]]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 32 of type GridBlazor.Pages.GridHeaderComponent`1[Model.EventNotification]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 21 of type Microsoft.AspNetCore.Components.CascadingValue`1[GridBlazor.Pages.GridComponent`1[Model.EventNotification]]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 33 of type GridBlazor.Pages.GridHeaderComponent`1[Model.EventNotification]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 22 of type Microsoft.AspNetCore.Components.CascadingValue`1[GridBlazor.Pages.GridComponent`1[Model.EventNotification]]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 34 of type GridBlazor.Pages.GridHeaderComponent`1[Model.EventNotification]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 23 of type Microsoft.AspNetCore.Components.CascadingValue`1[GridBlazor.Pages.GridComponent`1[Model.EventNotification]]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 35 of type GridBlazor.Pages.GridHeaderComponent`1[Model.EventNotification]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 24 of type Microsoft.AspNetCore.Components.CascadingValue`1[GridBlazor.Pages.GridComponent`1[Model.EventNotification]]
[15:50:50 0HLS5HOV76P1D:00000001 DBG] <Microsoft.AspNetCore.Components.RenderTree.Renderer> Rendering component 36 of type GridBlazor.Pages.GridHeaderComponent`1[Model.EventNotification]

...

And then after that it re-renders that component. For small components this isnt a problem, but for others that are for example, grids, it becomes a performance issue where simply clicking within the grid or any part of it will cause a complete re-render of the entire grid.

Is this intended?

Edit:

I realize I can override ShouldRender, but right now ShouldRender doesn’t contain any information as to where the call came from or how the request to render was raised. It would be handy, for example, if ShouldRender had some sort of parameter that would say "Hey, I am asking if I should render because of a MouseClickEvent or a DragEvent etc. Is this out-of-scope for what the team is envisioning for core and should the developer instead implement this on their own end?

Further technical details

  • ASP.NET Core version : 3.1
  • Include the output of dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.100
 Commit:    cd82f021f4

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17763
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.100\

Host (useful for support):
  Version: 3.1.0
  Commit:  65f04fb6db

.NET Core SDKs installed:
  2.1.700 [C:\Program Files\dotnet\sdk]
  3.0.100 [C:\Program Files\dotnet\sdk]
  3.1.100 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

  • Visual Studio 2019 16.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mrpmorriscommented, Dec 25, 2019

The @onclick should be on the thing you click to filter, not on the grid. If you make that filter UI a component too, the re-render should be on the component clicked + all components inside of it, not parent components. So just the filter, and not the grid.

Your scenario sounds very bad though. What is your real scenario?

1reaction
Oninaigcommented, Jan 10, 2020

@mrpmorris your suggestion definitely helps and I have reduced the redundant re-renders but a bunch. Thank you so much.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mouse events causing "Too many re-renders" in my ...
I am trying to use a ternary that changes my "titleHover" state variable based on mouse hovering an element. Unfortunately my code is...
Read more >
How I eliminate ALL unnecessary Rerenders in React
First, we will dig into what is causing these unnecessary renders and how we can spot these different causes easily. Afterward, we will...
Read more >
How to handle click events in React TypeScript
Learn how to use the MouseEvent type to handle click events in React ... This causes the component to re-render and display the...
Read more >
Event Bubbling and Event Catching in JavaScript and React
When an event is fired, React calls the proper element first (that is the Target Phase – element you clicked) then it starts...
Read more >
Handling Events
Each browser event handler is registered in a context. ... Clicks on the button cause that handler to run, but clicks on the...
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