OnClick callback does not work, it throws an exception in the frontend
See original GitHub issueDescribe the bug
Pretty much the title
Which Blazor project type is your bug related to?
- Server-Side I am using the release 1.0.2
Which charts does this bug apply to?
ChartJsLineChart but i haven’t tried other
To Reproduce
Steps to reproduce the behavior:
- Setup a OnClick callback on the chart LineConfig.LineOptions
- Go to the web browser (I tried Firefox and Chrome) and click on the chart
- A javascript error is thrown in the frontend:
TypeError: n.onClick.call is not a function 17 Chart.min.js:7:101051
handleEvent https://localhost:5001/_content/ChartJs.Blazor/Chart.min.js:7
eventHandler https://localhost:5001/_content/ChartJs.Blazor/Chart.min.js:7
i https://localhost:5001/_content/ChartJs.Blazor/Chart.min.js:7
<anonymous> https://localhost:5001/_content/ChartJs.Blazor/Chart.min.js:7
Expected behavior
OnClick callback should be called without throwing an exception
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Why is using onClick() in HTML a bad practice?
If you declare function doSomething(){} within an IIFE, then make doSomething() an element's onClick action in your HTML page, you'll get an ...
Read more >The 10 Most Common JavaScript Issues Developers Face
If you need help figuring out why your JavaScript isn't working, consult this list of the 10 most common JavaScript issues from a...
Read more >JavaScript button to call the Apex method is not working
The error I get is: onSuccess not defined in the callback. What am I doing wrong? apex · javascript-remoting.
Read more >Understanding common frustrations with React Hooks
We'll discuss the problem React Hooks intends to solve, what was wrong ... throw React Hook "useState" cannot be called inside a callback....
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

So I looked into this and found the issue. Basically, the
UpdateChart()function fromChartJsInterop.tswas incomplete. During an update, the newconfigis applied on top of the existingconfig, which broke the “wiring up” that is done duringSetupChartwhich is necessary to invoke.Netmethods asOnClick/OnHoverhandlers.This bug is only triggered when you register
.Nethandlers foronClick/onHoverand then you callUpdate()on the chart.@db85 Calling
_doughnutChartJs.Update()inOnInitialized()is dangerous if you consider the component’s lifecycle. The very first time there should be aNullPointerExceptionbecause the@refdirective doesn’t populate the_doughnutChartJsvariable until later (after the component is rendered). Due to prerendering, the lifecycle methods are called twice; the second time over there’s no more NPE and the call toUpdate()goes through, but then it triggers the bug.At least this is my current understanding of the issue right now.
To cut a long story short, I have a working version of a fix on this branch: https://github.com/mariusmuntean/ChartJs.Blazor/tree/IssueFix/54-OnClickCallback I expect to have a new release ready during the weekend. For anyone who cannot wait until then, a workaround would be to take the
ChartJsBlazorInterop.jsfrom myIssueFixbranch, add it to theirwwwrootfolder and reference that in theirindex.html/_Host.cshtmlinstead of the one provided by my library (I mean the one from_content).@mariusmuntean Thank you very much for the quick fix and the detailed answer. I have checked out your branch and it works. 👍