我使用React实现了Include组件。它从网址加载内容。 这个测试有效,但也会产生意外的无限循环,渲染...为什么?
<script type="text/jsx">
/** @jsx React.DOM */
var Include = React.createClass({
getInitialState: function() {
return {content: 'loading...'};
},
render: function() {
var url = this.props.src;
$.ajax({
url: url,
success: function(data) {
this.setState({content: data});
}.bind(this)
});
return <div>{this.state.content + new Date().getTime()}</div>;
}
});
var Hello = React.createClass({
render: function() {
return <Include src="hello.txt" />;
}
});
React.renderComponent(
<Hello />,
document.getElementById('hello')
);
</script>
答案 0 :(得分:1)
我意识到渲染已经执行了很多次,所以不是更好的地方我的ajax调用(-_-)'
这种方式很好:
<script type="text/jsx">
/** @jsx React.DOM */
var Include = React.createClass({
getInitialState: function() {
var url = this.props.src;
$.ajax({
url: url,
success: function(data) {
this.setState({content: data});
}.bind(this)
});
return {content: 'loading...'};
},
render: function() {
return <div>{this.state.content + new Date().getTime()}</div>;
}
});
var Hello = React.createClass({
render: function() {
return <Include src="hello.txt" />;
}
});
React.renderComponent(
<Hello />,
document.getElementById('hello')
);
</script>
感谢您阅读!