javascriptreactjsuse-strict

How to use undeclared variables in Reactjs


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.


Solution

  • 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>
    }