# How to add event handler in grafana 8 react panel plugins

**URL:** <https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668>\
**Category:** Grafana Plugin Development\
**Created:** [January 7, 2022, 9:50am UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668 "2022-01-07T09:50:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamed](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@hamed](https://community.grafana.com/u/hamed)\
**Post date:** [January 7, 2022, 9:50am UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668/1 "2022-01-07T09:50:40Z")

</div>

I’m developing custom panel plugin for grafana 8.3.3. I need to handle event after panel html has loaded, but I don’t know how can I register event in new version of grafana plugins. I found [This PanelEvents docs](https://grafana.com/docs/grafana/latest/packages_api/data/panelevents/) but I don’t know how can I use panel events. Could anyone provide me some example for doing this?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![marcusolsson](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/marcusolsson/32/7922_2.png) [@marcusolsson](https://community.grafana.com/u/marcusolsson)\
**Post date:** [January 7, 2022, 10:24am UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668/2 "2022-01-07T10:24:07Z")

</div>

Could you share more about what you’d like to do when the panel has loaded? What events are you interested in?

---

<div class="post-metadata">

**Author:** ![hamed](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@hamed](https://community.grafana.com/u/hamed)\
**Post date:** [January 7, 2022, 11:41am UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668/3 "2022-01-07T11:41:46Z")

</div>

Thank you for your response. I want to use [jquery jstree plugin](https://www.jstree.com/) in my grafana panel plugin. I need to first generate tree struture in nested `<ul><li>...</li></ul>` formats and after rendering html, I need to call `$("#tree").jstree()`. But I don’t know where can I call this function. Here is the example of my plugin code:

```auto
interface Props extends PanelProps<SimpleOptions> {}

export const SimplePanel: React.FC<Props> = ({ options, data, width, height }) => {
  
  return (

    <div >
      <div id="tree">
        <ul>
          <li>Root node 1</li>
          <li>Root node 2</li>
        </ul>
      </div>
      // I tried calling $("#tree").jstree() inside {} here but, but it seems the <div id="tree"> is undefined here
    </div>
  );
};

```

I’ve developed this plugin in older version of grafana(6) without any problem. But I don’t know How can I do this in this version. I know, maybe my approach is incorrect in this version, But I need to use jquery and I need to call jquery functions after complete rendering of html.

In older versions before 7, I could bind event to my panel in this way:

```auto
    this.events.on('render', this.onRender.bind(this));
    this.events.on('data-received', this.onDataReceived.bind(this));
    this.events.on('data-error', this.onDataError.bind(this));
    this.events.on('data-snapshot-load', this.onDataReceived.bind(this));
    this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
    ...

```

---

<div class="post-metadata">

**Author:** ![amotl](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/amotl/32/5891_2.png) [@amotl](https://community.grafana.com/u/amotl)\
**Post date:** [January 11, 2022, 12:04am UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668/4 "2022-01-11T00:04:16Z")

</div>

Hi,

I also would like to chime in here in order to signal strong interest on this topic.

We conceived the [GitHub - panodata/grafanimate: Animate timeseries data with Grafana](https://github.com/panodata/grafanimate) program the other day and just recently gave it some love, aiming to make it ready with recent versions of Grafana. However, we also discovered that the venerable `data-received`-type events were gone with the new internal architecture based on React. We also tried to fiddle a bit with the new [PanelEvents | Grafana Labs](https://grafana.com/docs/grafana/latest/packages_api/data/panelevents/), to no avail.

In general, we are wondering if using corresponding panel events with React would now need any kind of software component to link with the Grafana SDK/Toolkit in order to be able to use corresponding symbols?

I am asking this maybe naive question because, beforehand, it was easy to inject arbitrary Javascript code ([`grafana-studio.js`](https://github.com/panodata/grafanimate/blob/0.5.5/grafanimate/grafana-studio.js)) into the window’s scope in order to get hold of the active dashboard object \[1\]. On this object, we were able to install the same event handlers as outlined above by @hamed, using named events identified by plain text labels.

In order to get a synchronized signal after all panels successfully received their data, we made [`grafana-studio.js::onDashboardRefresh`](https://github.com/panodata/grafanimate/blob/0.6.0/grafanimate/grafana-studio.js#L231-L291) capture the `data-received`, `data-frames-received`, `data-error` and `dashboard-fetch-end` events from all panels and synthesize an `all-data-received` event from it.

With this machinery in place, `grafanimate` was able to efficiently produce animations. Currently, we are working around it by having introduced a mechanism to [Add parameter "--exposure-time", defaulting to 0.5 seconds · panodata/grafanimate@1f183d1 · GitHub](https://github.com/panodata/grafanimate/commit/1f183d1bc), which the user would have to adjust appropriately. This definitively involves quite an amount of trial and error in order to get the right “exposure time” in different scenarios, as it very much depends on the number of panels and response times from the underlying databases.

As we didn’t follow the evolution of the Grafana code base into React land closely, and we might lack some essential React skills to get an idea about how to use the new `PanelEvents`, maybe you can share some resources where we would be able learn more about them?

Thank you already and with kind regards,  
Andreas.

* * *

1. `angular.element("grafana-app").injector().get("dashboardSrv").getCurrent()`

---

<div class="post-metadata">

**Author:** ![marcusolsson](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/marcusolsson/32/7922_2.png) [@marcusolsson](https://community.grafana.com/u/marcusolsson)\
**Post date:** [January 13, 2022, 10:16am UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668/5 "2022-01-13T10:16:21Z")

</div>

It looks like you could use the `useEffect` and `useRef` hook to get a reference to the HTML element.

Haven’t tested it, but you should be able to do something like this:

```auto
const treeContainer = useRef(null);

```

```auto
useEffect(
  () => {
    if (treeContainer.current) {
      treeContainer.current.jstree();
    }
  },
  [treeContainer.current])

```

Maybe this article on [Simple D3 with React Hooks](https://medium.com/@jeffbutsch/using-d3-in-react-with-hooks-4a6c61f1d102) can give you some inspiration.

---

<div class="post-metadata">

**Author:** ![marcusolsson](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/marcusolsson/32/7922_2.png) [@marcusolsson](https://community.grafana.com/u/marcusolsson)\
**Post date:** [January 13, 2022, 10:42am UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668/6 "2022-01-13T10:42:01Z")

</div>

That’s a cool looking tool! 😯

I haven’t had a chance to play around with it yet, but the React platform has an event bus that you could look into. It’s available from the panel props, `props.eventBus`, in recent versions of Grafana.

For an example on how to subscribe to events, you can check out the pie chart panel:

> <https://github.com/grafana/grafana/blob/01dd623daa804d2f09a71a075dd4df398361cce7/public/app/plugins/panel/piechart/PieChartPanel.tsx#L164-L165>

---

<div class="post-metadata">

**Author:** ![amotl](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/amotl/32/5891_2.png) [@amotl](https://community.grafana.com/u/amotl)\
**Post date:** [January 13, 2022, 1:03pm UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668/7 "2022-01-13T13:03:41Z")

</div>

Dear Marcus,

thank you for sharing this pointer. Apologies for hijacking this discussion, @hamed. I hope this helps you as well. Otherwise, feel free to move our discussion into another thread, @marcusolsson.

### Introduction

I already tried to play around a bit with the React event bus, but most probably did it wrong by poking around without knowing anything.

I will definitively look into this on the next iteration. In order to prepare myself as good as possible, I am humbly adding some additional questions on this matter.

### Subscribing to events

Do you have any insights if it would be possible to resolve events (`eventBus.getStream(DataHoverEvent)`) by plain strings instead of typed symbols?

According to the definition of `DataHoverEvent`,

> <https://github.com/grafana/grafana/blob/01dd623daa804d2f09a71a075dd4df398361cce7/packages/grafana-data/src/events/common.ts#L24-L26>

it would be sweet if, for example, `eventBus.getStream("data-hover")` actually worked \[1\]. If that would not work, I guess I would need another way to get hold of a reference to the corresponding event class from the `grafana-data` package. Please educate me on this matter: I believe there is no way to `import` or `require` arbitrary symbols from plain Javascript at runtime, right?

### Figuring out the right events

For most of its current business, grafanimate is interested in events around the initialization and data-loading phases and lifecycles.

> [@amotl](#):
>
> In order to get a synchronized signal after all panels successfully received their data, we made [`grafana-studio.js::onDashboardRefresh`](https://github.com/panodata/grafanimate/blob/0.6.0/grafanimate/grafana-studio.js#L231-L291) capture the `data-received` , `data-frames-received` , `data-error` and `dashboard-fetch-end` events from all panels

Looking at [grafana/packages/grafana-data/src/events at main · grafana/grafana · GitHub](https://github.com/grafana/grafana/tree/main/packages/grafana-data/src/events), I remember not having been able to quickly figure out the appropriate event names. Combined with grafanimate’s needs to subscribe to events identified by plain string symbols as outlined above, I quickly lost track – probably also because it already was late at night.

Do you know if the React event bus also accepts wildcard subscriptions? That would enable users naively poking around like me to explore the possible event types (and their sequence flow) interactively. Otherwise, I will also be curious to discover a complete list of events you might be maintaining elsewhere because I well might have missed it due to lack of time to research this topic thoroughly enough.

### Connecting the dots

I am reading a bit through those patches submitted by @torkel, thank you.

- [https://github.com/grafana/grafana/pull/21010](https://github.com/grafana/grafana/pull/21010)
- [https://github.com/grafana/grafana/pull/27564](https://github.com/grafana/grafana/pull/27564)

Coming from there, after further searching the code base, I believe that those legacy `PanelEvents` is what I would like to unlock again.

> <https://github.com/grafana/grafana/blob/8114f6b06/packages/grafana-data/src/types/legacyEvents.ts#L17-L29>

Thanks again for listening to my needs.

With kind regards,  
Andreas.

* * *

1. I know `grafanimate` takes an unorthodox approach by injecting its `grafana-studio.js` as plain Javascript into the page instead of shipping a corresponding plugin which the user would install into Grafana. However, this makes it operate on any Grafana instance without further ado.

---

<div class="post-metadata">

**Author:** ![marcusolsson](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/marcusolsson/32/7922_2.png) [@marcusolsson](https://community.grafana.com/u/marcusolsson)\
**Post date:** [January 17, 2022, 12:46pm UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668/8 "2022-01-17T12:46:26Z")

</div>

I just published a small guide for subscribing to application events:

> [@How to subscribe to Grafana application events](https://community.grafana.com/t/how-to-subscribe-to-grafana-application-events/59147):
>
> If you’re building a panel plugin, Grafana already provides the data you need to configure and render your panel. If not, you can retrieve the information you need from any of the runtime services, like BackendSrv and TemplateSrv. In some cases though, you want your plugin to react to changes outside of your plugin. For example, when the user hovers their cursor over data in another panel. In this post, you’ll learn how to make your plugin react to events in Grafana. Note: If you’ve previously…

Let me know if there’s anything I missed!

---

<div class="post-metadata">

**Author:** ![amotl](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/amotl/32/5891_2.png) [@amotl](https://community.grafana.com/u/amotl)\
**Post date:** [January 17, 2022, 10:23pm UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668/9 "2022-01-17T22:23:49Z")

</div>

> [@marcusolsson](#):
>
> I just published a small guide for subscribing to application events. Let me know if there’s anything I missed!

Thank you very much, Marcus! I will try to apply the corresponding insights to the next iteration of `grafanimate`. I am sure others from the community will also find this resource very valuable.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/grafana/original/2X/e/ef5308cc58c9e5a13187b5012eb99f406928462f.png) [@system](https://community.grafana.com/u/system)\
**Post date:** [January 17, 2023, 10:24pm UTC](https://community.grafana.com/t/how-to-add-event-handler-in-grafana-8-react-panel-plugins/58668/10 "2023-01-17T22:24:12Z")

</div>

Closed on January 17, 2023, 10:24pm UTC

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
