How to use undeclared variables in Reactjs

176 Views Asked by At

I am creating a script node in my react code on run time using document.createElement('script') and assigning its src to a url, Now I want to use the variable of the included JS in my react code without declaring it.

1

There are 1 best solutions below

0
Halil İbrahim Özdoğan On BEST ANSWER

You can simply define to the global like window.yourVariable = 'value' and then you can use.

window.globalVariable = {
   foo: 'bar',
};

const Component = () => {
   // you can access in here.
   return <div></div>
}