Search code examples
javascriptpythonreactjspymongotornado

How to pass JS variables to JSX files


I'm using mongo, tornado, and react.

from the tornado server I got my data, I sent it to the html template and then parsed it to JSON, now I want to use this variable "obj" and pass it down to my jsx file, how can I do it?

this is my js code and the script to my react: enter image description here

and this my JSX file: enter image description here


Solution

  • You can use the window global variable (which is accessed from anywhere in your code).

    In your tornado template you should use:

    <script>
        window.obj = JSON.parse(text);
    </script>
    

    And inside your jsx file you can access that variable from the window object:

    render() {
        console.log(window.obj);
    }