简体   繁体   English

React.js:将React.js代码与HTML分开

[英]React.js: Separate React.js code from HTML

I am new to React.js and just trying it out. 我是React.js的新手,只是尝试一下。 I have a code in my index.html as follow: 我在index.html中有一个代码,如下所示:

 <script type="text/babel"> var galleryData = [ { smallURL: "https://farm6.static.flickr.com/5650/22151329006_9d32e48fe9_s.jpg", bigURL: "https://farm6.static.flickr.com/5650/22151329006_9d32e48fe9_b.jpg", title: "Nothing Gold Can Stay" }, { smallURL: "https://farm6.static.flickr.com/5650/22151329006_9d32e48fe9_s.jpg", bigURL: "https://farm6.static.flickr.com/5650/22151329006_9d32e48fe9_b.jpg", title: "Nothing Gold Can Stay"} ]; var ThumbNail = React.createClass({ render: function(){ return (<a href={this.props.data.bigURL} title={this.props.data.title} data-gallery=""><img src={this.props.data.smallURL} /></a>); } }); .... </script> 

My question is simple yet I am too dumb to figure it out: Can I separate the whole script inside this tag into another file, and reference it like how we do it with javascript file in a normal way? 我的问题很简单,但是我却太笨了,无法弄清楚:我可以将这个标签内的整个脚本分成另一个文件,然后像使用JavaScript文件一样以正常方式引用它吗?

If yes, what is the extension of this file and how can I reference it in my HTML? 如果是,此文件的扩展名是什么,如何在HTML中引用它?

You can, and should, put your JavaScript into separate files. 您可以并且应该将JavaScript放入单独的文件中。 For React, the majority of people write their code in JSX using the file extension .jsx . 对于React,大多数人都使用文件扩展名.jsx在JSX中编写代码。

As for how to link it up, I suggest https://facebook.github.io/react/docs/getting-started.html#separate-file . 至于如何进行链接,我建议https://facebook.github.io/react/docs/getting-started.html#separate-file Facebook has done a very good job with their documentation. Facebook在其文档方面做得非常出色。

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

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