错误Uncaught TypeError:inst.render不是指向行号的函数
function RenderComment(){
alert("Clicked");
ReactDOM.render(React.createElement("div", {}, React.createElement(Comment,{})), //the error points to this line
document.getElementById('content')
);
}
我也收到警告
警告:注释(...):在返回的组件实例上找不到render
方法:您可能忘记定义render
,从无状态组件返回null / false,或尝试渲染一个元素,其类型是一个函数,它不是反应组件。
我的整个React JS代码
<script type="text/babel">
var CommentBox = React.createClass({
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
var initializeScroll = 0;
loadCommentsFromServer: function() {
initializeScroll = initializeScroll + 5;
var data = {
"count": initializeScroll
};
$.ajax({
url: this.props.url,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify(data),
cache: false,
beforeSend: function(xhr) {
xhr.setRequestHeader(header, token);
},
success: function(result) {
this.setState({
data: this.state.data.concat(result)
});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
getInitialState: function() {
window.addEventListener("scroll", this.handleScroll);
return {
loadingFlag: false,
data: []
};
},
componentDidMount: function() {
this.loadCommentsFromServer();
},
handleScroll: function(e) {
//this function will be triggered if user scrolls
var windowHeight = $(window).height();
var inHeight = window.innerHeight;
var scrollT = $(window).scrollTop();
var totalScrolled = scrollT + inHeight;
alert("Heeeeey");
if (totalScrolled + 100 > windowHeight) { //user reached at bottom
if (!this.state.loadingFlag) { //to avoid multiple request
this.setState({
loadingFlag: true,
});
loading("on");
this.loadCommentsFromServer();
}
}
},
render: function() {
return ( < div className = "commentBox" >
< CommentList data = {
this.state.data
}
/> < /div>
);
}
});
var CommentList = React.createClass({
render: function() {
var commentNodes = this.props.data.map(function(comment) {
return ( < Comment > {
comment
} < /Comment>
);
});
return ( < div className = "commentList" > {
commentNodes
} < /div>);
}
});
var Comment = React.createClass({
rawMarkup: function() {
var rawMarkup = marked(this.props.children.toString(), {
sanitize: true
});
return {
__html: rawMarkup
};
},
render: function() {
return ( < div className = "comment" >
< span dangerouslySetInnerHTML = {
this.rawMarkup()
}
/> < /div>
)
}
});
ReactDOM.render( < CommentBox url = "/myUrl/abcd" / > ,
document.getElementById('content')
);
<script>
我要做的是,滚动网页(无限滚动)并从服务器加载内容 - AJAX有点事+ JQuery(用于无限scroll_ + React JS。 请帮我。我度过了这整个周末。