在 React 教程中,我正在查看 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 列为依赖项。当我在 react 存储库中搜索 ajax
时,没有该函数的任何声明。
这个 $.ajax
函数是 React 特定的函数吗?它是不是 jQuery 以外的东西?感谢您帮助我们更好地理解 React 的这一部分是如何工作的!
最佳答案
React 不需要 需要 jQuery 才能工作。如果你看this page from tutorial website (向上滚动一点)明确提到包含 jQuery 只是为了简化。
这里也包括注释
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.
还有 check out the complete section on getting started具有包含 jQuery 的主索引 html 页面。
希望对你有帮助
关于javascript - React 是如何在不需要 jQuery 的情况下允许 $.ajax 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31149079/