[英]Trigger onClick event for a ReactJS element
I have a list of elements that are loaded with reactjs and at the end of that list there is a button that loads more items via onclick event using reactjs. 我有一个用reactjs加载的元素列表,在该列表的末尾有一个按钮,可通过使用reactjs的onclick事件加载更多项目。
I want to create a function that using javascript or jquery, trigger the onclick event to load all the items instead of clicking one by one on the load more items. 我想创建一个使用javascript或jquery的函数,触发onclick事件以加载所有项目,而不是在加载更多项目上一个接一个地单击。
I tried to do it using a interval in jquery but the $element.trigger('click')
is not working, does nothing. 我尝试使用jquery中的间隔来执行此操作,但是
$element.trigger('click')
无法正常工作,什么也不做。
Can anyone help me with this? 谁能帮我这个? please.
请。
ReactJS: ReactJS:
var ConversationShowMore = React.createClass({
getInitialState: function(){
return {show: false, next_comments: ""};
},
loadMoreComments: function(){
this.setState({show: true});
},
render: function(){
var obj = this.props.next_comments || "";
if (obj != "" && requesturl != obj) {
if (this.state.show) {
return (
<ConversationBox url={this.props.next_comments} />
)
}else{
return (
<a onClick={this.loadMoreComments} className="showmoreconversations" href="#" role="button"><span>Load more conversations...</span></a>
)
}
}else{
return (
<div></div>
)
}
}
});
Javascript/jQuery: Javascript / jQuery:
var tid = setInterval(myCode, 5000);
function myCode() {
if($("#conversationContainer a.showmoreconversations").length){
$("#conversationContainer a.showmoreconversations").trigger('click');
}else{
abortTimer();
}
}
function abortTimer() {
clearInterval(tid);
}
When component is mounted, you will trigger request to load more comments. 装入组件后,您将触发请求以加载更多注释。 When this request is complete, you schedule another request in X miliseconds.
此请求完成后,您将在X毫秒内安排另一个请求。
loadMoreComments(){
console.log('loaded more comments');
// Probably, you will trigger async request. Call following line when this request is complete.
this.timeout = window.setTimeout(this.loadMoreComments, 5000);
},
componentDidMount() {
this.loadMoreComments();
},
Remember to cancel scheduled request when unmounting component. 卸载组件时,请记住要取消预定的请求。 Otherwise, it will run virtually forever (and will surely case exception to be thrown)
否则,它将几乎永远运行(并且肯定会抛出异常)
componentWillUnmount() {
window.clearTimeout(this.timeout);
},
Working example over here: https://jsfiddle.net/69z2wepo/34030/ 这里的工作示例: https : //jsfiddle.net/69z2wepo/34030/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.