简体   繁体   English

如何将JS变量传递给JSX文件

[英]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? 从龙卷风服务器上获取数据,我将其发送到html模板,然后将其解析为JSON,现在我想使用此变量“ obj”并将其传递给我的jsx文件,该怎么办?

this is my js code and the script to my react: 这是我的js代码和对我的反应的脚本: 在此处输入图片说明

and this my JSX file: 这是我的JSX文件: 在此处输入图片说明

You can use the window global variable (which is accessed from anywhere in your code). 您可以使用window全局变量(可从代码中的任何位置访问)。

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: jsx文件中,您可以从window对象访问该变量:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM