0

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.

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.

I tried to do it using a interval in jquery but the $element.trigger('click') is not working, does nothing.

Can anyone help me with this? please.

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:

    var tid = setInterval(myCode, 5000);
    function myCode() {
        if($("#conversationContainer a.showmoreconversations").length){
            $("#conversationContainer a.showmoreconversations").trigger('click');
        }else{
            abortTimer();
        }
    }
    function abortTimer() {
        clearInterval(tid);
    }
3
  • Please, post minimal amount of code needed to reproduce your use case. Commented Mar 8, 2016 at 22:31
  • 1
    The idea of React is mapping data into DOM. The idea of jQuery is manipulating existing DOM. You're trying to mix two approaches, that won't make any good to your project. Just use this.loadMoreComments until it returns an empty set or null or whatever, in componentWillMount, and push new data into component state per each response. Commented Mar 8, 2016 at 22:47
  • Ok, I got it! makes a lot of sense. Commented Mar 8, 2016 at 23:42

1 Answer 1

1

When component is mounted, you will trigger request to load more comments. When this request is complete, you schedule another request in X miliseconds.

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, this is a lot more easy to implement, I already did this and it's working, thanks a lot Andreyco!
Actually, React is pretty simple & easy to learn. Practice, you will find simple solutions to anything later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.