How to listen for when a variable changes?

Hello,

How Can I listen on grafana variables change from my custom plugin code that is implemented in react ? I need to update my custom panel according to a variable changes.

I didn’t find any documentation regarding this topic.

I am using Grafana 8.1 on Windows 10.

Thanks in advance.

1 Like

Hello,

Same question here. I have a frontend panel plugin which updates the variables, and I have a backend data source plugin(which also has a frontend React part) in which I want to listen to those variables and send them to the backend.

I tried with getTemplateSrv and then getVariables but for some reason its returning me some random variables I had a few days ago, not the current ones which is very odd.

Have you managed to solve this? I’m browsing the community and came upon this post which is identical to my situation. Thanks in advance!

Panels that interpolate template variables should update whenever the value changes.

How to interpolate a template variable depends on whether it’s a panel or a data source.

@igorkujacic12 I’m not sure why you’re getting old values though. How are you retrieving the interpolated values?

Thanks for answering! My pc probably got messed up from spamming changes, when I reinstalled Grafana and made new variables the problem with old variables went away.

I have a custom panel that I wish to update the variables with(that part works fine). The other plugin is a custom backend data source made with springboot/react, and I would like to get those values from the variables on the backend as Strings so I was trying with Text box variables.

Since I’m a backend guy, I noticed that what I’m doing isn’t right. I put the code for getting the variables in the QueryEditor.tsx since from there I was already sending some input values to backend with queries(in the Json part of the Query). I used getTemplateSrv().getVariables(). And that works fine, whenever I go in Edit view of the data source, where I trigger the QueryEditor, it gets sent back.

The problem is when I’m in the dashboard, it doesn’t pick up variable changes and that makes sense, since its in QueryEditor :smiley: If I update the variable from the dashboard, the query in the data source runs again, but it doesn’t pick up the updated values.
Do you have any advice on this, am I using the wrong type of variable(am I supposed to use query variables)? Or am I simply doing it wrong