Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
Ajax call loading everytime on page reload duplicating content

Basically I load my homepage and one of my components is the following:

import React, {Component} from 'react';

class BlogPostsWidget extends Component {
  render() {
    $.ajax({
      type: "GET",
      url: 'https://example.com/wp-json/wp/v2/posts?filter[orderby]=asc&filter&per_page=3&categories=1,2,3,11,258,378,447',
      dataType: 'json',
      success: function(data) {
        data.forEach(function(post) {
          $( '.blog__body' ).append( '<div class="col-md-4"><div class="blog__body__article"><a target="_blank" href="' + post.link + '"><img class="card-img-top img-responsive lazy" src="' + post.ccw_thumbnail + '"></a><h2 class="bold text-center card-title">' + post.title.rendered + '</h2><div class="short-border"></div>' + post.excerpt.rendered + '<a target="_blank" class="black-link" href="' + post.link + '">READ MORE <i class="fa fa-chevron-right"></i></a></div>' );
        });
      }
    });

    return (
      <div className="blog">
        <div className="blog__body">
        </div>
      </div>
    );
  }
}
export default BlogPostsWidget;

On initial page load everything is working fine. However if i click on my logo to re-render my homepage my Ajax call is firing again causing the same 3 blog posts to be loaded in after my already rendered ones

How do I make sure my Ajax script runs only once?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use componentDidMount lifecycle method, and put the ajax call inside that. It will get called only once just after the component rendering.

From DOC:

componentDidMount() is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!