最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How is it that React allows $.ajax without requiring jQuery? - Stack Overflow

programmeradmin2浏览0评论

In the React tutorial, I'm looking at the example.js file

The file gets ments from a server in this function:

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...

However, there is no require('jquery') in the code. React doesn't list jQuery as a dependency. When I search for ajax in the react repository there isn't any declaration of that function.

Is this $.ajax function a react-specific function? Is it something other than jQuery? Thanks for any help to better understand how this part of React works!

In the React tutorial, I'm looking at the example.js file

The file gets ments from a server in this function:

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...

However, there is no require('jquery') in the code. React doesn't list jQuery as a dependency. When I search for ajax in the react repository there isn't any declaration of that function.

Is this $.ajax function a react-specific function? Is it something other than jQuery? Thanks for any help to better understand how this part of React works!

Share Improve this question asked Jun 30, 2015 at 21:45 YPCrumbleYPCrumble 28.8k25 gold badges111 silver badges175 bronze badges 1
  • 2 $ is just a variable name, anyone can define it to be anything... – dandavis Commented Jun 30, 2015 at 21:47
Add a ment  | 

2 Answers 2

Reset to default 7

React DOES NOT require jQuery to work. If you look at this page from tutorial website(scroll a bit above) it is clearly mentioned that jQuery is included only to simplify.

Including the note here too

Note: We included jQuery here because we want to simplify the code of our future ajax calls, but it's NOT mandatory for React to work.

Also check out the plete section on getting started that has the main index html page which includes jQuery.

Hope this helps

its being required in the index.html file. youll notice it gets required before the example.js file, which means jquery is available by the time the react code gets executed

发布评论

评论列表(0)

  1. 暂无评论