Elon Salfati
Elon Salfati

Reputation: 1687

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

Upvotes: 1

Views: 1847

Answers (1)

Dekel
Dekel

Reputation: 62676

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);
}

Upvotes: 2

Related Questions