2015-06-30 66 views
1

在阵营教程中,我期待在example.js文件

文件从服务器获取评论在这个函数:

var CommentBox = React.createClass({ 
    loadCommentsFromServer: function() { 
    $.ajax({ 
     url: this.props.url, 
     dataType: 'json', 
     cache: false, 
     success: function(data) { 
     this.setState({data: data}); 
     }.bind(this), 
     error: function(xhr, status, err) { 
     console.error(this.props.url, status, err.toString()); 
     }.bind(this) 
    }); 
    }, 
// Rest of CommentBox... 

但是,在代码中没有require('jquery')。 React不会将jQuery列为依赖项。当我在反应库中搜索ajax时,没有任何该函数的声明。

这是$.ajax功能反应特定的功能?是jQuery以外的东西吗?感谢您的帮助,以便更好地了解React的这部分工作原理!

+2

'$'只是一个变量名,任何人都可以将它定义为任何东西...... – dandavis

回答

7

反应是不是需要jQuery的工作。如果你看看this page from tutorial website(上面的滚动一下),很明显提到jQuery的包含只是为了简化。

包括这里还要注意

注:我们包括jQuery的这里,是因为我们要简化 我们未来的AJAX调用的代码,但它不是强制性的应对工作。

check out the complete section on getting started有主要索引html页面,其中包括jQuery。

希望这有助于

2

其在index.html文件所需的存在。你会注意到它在example.js文件之前是必需的,这意味着在反应代码执行时jQuery是可用的。

相关问题