Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Node.js

    Node.js Lesson 11: Echo Server

    JavaScript

    An Introduction to Clustering in Node.js

    JavaScript

    Create simple POS with React.js, Node.js, and MongoDB #16: Order Screen

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / React / React and AJAX – The Art of Fetching Data in React
    JavaScript

    React and AJAX – The Art of Fetching Data in React

    Supun KavindaBy Supun KavindaJanuary 6, 2020Updated:May 26, 2024No Comments12 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    React and AJAX – The Art of Fetching Data in React
    React and Ajax
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    React and AJAX – The Art of Fetching Data in React

    While creating the Hyvor Talk commenting plugin, I worked a lot of time with React and AJAX, because its moderation panel was completely written with those technologies. In this article, I’ll reveal everything I learned about React and AJAX. When you grasp the basics and some other tips, it’s really easy to write AJAX-based React applications. Let’s start!

    Are you ready to start the tutorial?

    AJAX

    AJAX is a web technology used in client-side to perform an asynchronous HTTP request. Simply, we can use AJAX to fetch data from a server without reloading a page (If you don’t like the term AJAX, remember is as fetching data from APIs, no rocket science).

    React and AJAX

    If you have previously worked with jquery or angular, you may know that those libraries/frameworks have ways to fetch data (or use AJAX) by default. However, React doesn’t even know it has to deal with servers at all. React is always about rendering components. It takes data from two sources which are props and state. We can use AJAX to fetch data from the server to React. Then, we can render the component based on the loaded data.

    Choosing an AJAX Library

    First, let’s choose the method we use for asynchronous HTTP requests. The only way of performing asynchronous HTTP requests from the browser is using the XMLHttpRequest interface (Only new browsers support fetch API). However, there are wrappers that make the task much easier. Choosing the best one is completely upon you. It is all about how clear the syntax for you. For some methods, make sure you are happy with the browser support. Here are the methods and their syntax.

    1. XMLHttpRequest This is the default method which is supported by all major browsers. The methods below (excluding Fetch API) is based on this.
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
          if (this.readyState == 4 && this.status == 200) {
             // ajax request is succesful
          }
      };
      xhttp.open("GET", "SOME_URL", true);
      xhttp.send();
      
    2. Fetch API (This doesn’t support IE) There are two drawbacks of using fetch API: Less browser support and lack of feature to cancel the request.
      fetch('SOME_URL', {
          method: 'get'
      }).then(function(response) {
           // success
      }).catch(function(err) {
           // Error 
      });
      
    3. Axios Axios makes XMLHttpRequests from the browser. Many React developers prefer this as it perfectly pairs with React.
      axios.get('/user?ID=12345')
        .then(function (response) {
          // handle success
          console.log(response);
        })
        .catch(function (error) {
          // handle error
          console.log(error);
        })
        .finally(function () {
          // always executed
      });
      
    4. SuperAgent SuperAgent is another promising agent. It can be written in different ways: callback, promise with then/catch, and promise with async/await.
      const superagent = require('superagent');
      
      // callback
      superagent
        .post('/api/pet')
        .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
        .set('X-API-Key', 'foobar')
        .set('accept', 'json')
        .end((err, res) => {
          // Calling the end function will send the request
        });
      
      // promise with then/catch
      superagent.post('/api/pet').then(console.log).catch(console.error);
      
      // promise with async/await
      (async () => {
        try {
          const res = await superagent.post('/api/pet');
          console.log(res);
        } catch (err) {
          console.error(err);
        }
      })();
      

    Did you choose your favorite library? Let’s continue to the next part.

    In this tutorial, I chose the Programming Quotes API and XMLHttpRequest. We can fetch some programming quotes from the API and display it to the user. Until loading, we can show a loader or a “loading” message.

    Here’s the complete code of the first step. Check it out. I’ll explain all the steps in a moment.

    A simple HTML div element where we render the React DOM.

    <div id="view"></div>
    

    Then, the React code.

    class ProgrammingQuotes extends React.Component {
    
        constructor(props) {
            super(props);
    
            this.state = {
                isLoaded: false,
                error: null,
                quotes: []
            };
          }
    
        componentDidMount() {
    
            var xhr = new XMLHttpRequest();
    
            xhr.addEventListener("readystatechange", () => {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        // request succesful
                        var response = xhr.responseText,
                            json = JSON.parse(response);
    
                      this.setState({
                        isLoaded: true,
                        quotes: json
                       });          
                    } else {
                        // error
                      this.setState({
                        isLoaded: true,
                        error: xhr.responseText
                      });
                    }
                }
            });
    
            xhr.open("GET", "https://programming-quotes-api.herokuapp.com/quotes/page/1", true);
            xhr.send();
    
          }
    
          render() {
    
              var body;
    
            if (!this.state.isLoaded) {
                // yet loading
              body = <div>Loading...</div>;
            } else if (this.state.error) {
                // error
              body = <div>Error occured: { this.state.error }</div>
            } else {
                // success
    
                var quotes = this.state.quotes.map(
                    quote => <div key={quote.id} className="quote-view">{ quote.en }</div>
                  );
    
                body = <div>{quotes}</div>
            }
    
            return body;
          } 
    }
    
    ReactDOM.render(
        <ProgrammingQuotes />,
      document.getElementById('view')
    );
    

    (If you like to test it our first, here’s the JSFiddle Demo)

    Let’s go step by step.

    1. Setting up State

    constructor(props) {
          super(props);
    
          this.state = {
            isLoaded: false,
              error: null,
              quotes: []
        };
    }
    

    If you are a React dev, you must have used super(props). If you don’t know the reason, check out this blog post. However, the main focus of declaring the constructor is setting up the state variables. If you don’t have any other function to execute in the constructor, you can simply declare the state as property.

    Read More:  Fortune 500 Top Employment trend 2019: Virtual Workforce

    Ex:

    class ProgrammingQuotes extends React.Component {
        state = {
            isLoaded: false,
              error: null,
              quotes: []
        };
    }
    

    Here we declare three states to save load status, error, and actual data. Usually, these are the only states needed for a component that fetches data and displays in itself. When the component tree grows, things can become complicated.

    In non-react apps, we fetch from APIs on event handlers such as onload, onclick, etc. Nevertheless, React is about components and component trees. Also, as react doesn’t have external states like Redux, we have to rely on local states.

    2. Fetching Data

    Remember the name componentDidMount.

    It is where all this happens.

    componentDidMount() method runs after the component has been rendered to the DOM.

    it is the best place to fetch data!

    componentDidMount() {
    
        var xhr = new XMLHttpRequest();
    
        xhr.addEventListener("readystatechange", () => {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    // request successful
                    var response = xhr.responseText,
                        json = JSON.parse(response);
    
                    this.setState({
                        isLoaded: true,
                        quotes: json
                    });          
                } else {
                    // error
                    this.setState({
                        isLoaded: true,
                        error: xhr.responseText
                    });
                }
            }
        });
    
        xhr.open("GET", "https://programming-quotes-api.herokuapp.com/quotes/page/1", true);
        xhr.send();
    
    }    
    

    In this method, we initiate the AJAX request using XMLHttpRequest. We use the GET method and https://programming-quotes-api.herokuapp.com/quotes/page/1 as the endpoint which returns a JSON output of first page quotes.

    readystatechange handler is called when the readyState of the request changes. readyState 4 means the request is completed. Normally, servers set the http response code to 200 when the request is successful (Thankfully, our API does it too). So, if xhr.status is 200, we know that the request is successful. Next, we parse the response to JSON and update the state.

    If the request is not successful, we set the this.state.error variable which we will use in the next step.

    3. Rendering the Component

    render() {
    
        var body;
    
        if (!this.state.isLoaded) {
            // yet loading
          body = <div>Loading...</div>;
        } else if (this.state.error) {
            // error
          body = <div>Error occured: { this.state.error }</div>
        } else {
            // success
    
              var quotes = this.state.quotes.map(
                  quote => <div key={quote.id} className="quote-view">{ quote.en }</div>
              );
    
            body = <div>{quotes}</div>
        }
    
          return body;
    }
    

    In our component we have three states:

    1. Loading
    2. Loaded, but an error occurred
    3. Loaded, successful

    Here we use a simple condition to render different UIs based on the current state.

    1. We show a message saying “Loading…” when the data is loading
    2. If something goes wrong, we will show the error message.
    3. If data is fetched, we will render the quotes looping through each quote. Check out React lists and keys for more details on creating lists.And, it’s important to find out how the response of the API is structured. To do this, you can check out either their documentation or response itself. I use the browser console or an online JSON Pretty Printer to examine the structure of the response.

    PRO TIP: render() method runs every time when the state is changed using setState().

    Determining the Best Component for Fetching

    In the above example, we only had one component. However, in real-world applications, we will have many complex component trees and we have to determine the best component to do the fetching. The following image shows a bit complex React component tree.

    React Component Tree
    React Component Tree

    The components indicated with red dot (quite like the Japan flag) needs the data from API to be rendered. So, the component with the blue dot is the best place to fetch the data.

    TL;DR Find all the components that depend on the data from API (including where the loader and the error message is shown). Then, get their lowest common parent. That’s the best component to fetch data.

    Using Props Too

    Now, we are going to upgrade our single component into a simple app where users can select quotes from multiple pages. I came up with this idea because our API natively supports that (use /quotes/page/${PAGE_ID} for different pages).

    Read More:  Amazon S3 Cloud Storage Proxying Through NodeJS from Angular Frontend Securely

    Here’s our upgraded app. (JSfiddle here)

    class App extends React.Component {
    
        state = {
            currentPageNumber: 1
        }
    
        onChange(pageNumber) {
            this.setState({currentPageNumber: pageNumber});
        }
    
        render() {
    
            return (
                <div>
                    <PageNumberSelector 
                        onChange={(val) => this.onChange(val)}
                        currentPageNumber={this.state.currentPageNumber}
                    />
                    <ProgrammingQuotes currentPageNumber={this.state.currentPageNumber} />
                </div>
            );
        }
    
    }
    
    function PageNumberSelector(props) {
    
        var options = [1,2,3,4,5].map((i) => {
    
            return (
                <option
                    value={i} 
                    key={i}
                >{i}</option>
            );
    
        });
    
        return (
            <div>
                Select Page Number: 
                <select 
                    value={props.currentPageNumber}
                    onChange={(e) => props.onChange(e.target.value)}
                >
                    { options }
                </select>
            </div>
        )
    }
    
    
    class ProgrammingQuotes extends React.Component {
    
        constructor(props) {
            super(props);
    
            this.state = {
                isLoaded: false,
                error: null,
                quotes: []
            };
        }
    
        componentDidMount() {
    
            var xhr = new XMLHttpRequest();
    
            xhr.addEventListener("readystatechange", () => {
                    if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                    // request succesful
                    var response = xhr.responseText,
                            json = JSON.parse(response);
    
                this.setState({
                    isLoaded: true,
                    quotes: json
                 });          
            } else {
                    // error
                this.setState({
                    isLoaded: true,
                    error: xhr.responseText
                });
            }
                    }
            });
    
            var page = this.props.currentPageNumber;
    
            xhr.open("GET", "https://programming-quotes-api.herokuapp.com/quotes/page/" + page, true);
            xhr.send();
    
        }
    
        render() {
    
            var body;
    
            if (!this.state.isLoaded) {
                    // yet loading
                body = <div>Loading...</div>
            } else if (this.state.error) {
                    // error
                body = <div>Error occured: { this.state.error }</div>
            } else {
                    // success
    
                var quotes = this.state.quotes.map(quote => <div key={quote.id} className="quote-view">{ quote.en }</div>);
    
                    body = <div>{quotes}</div>
            }
    
            return body;
        } 
    
    }
    
    ReactDOM.render(
        <App />,
        document.getElementById('app')
    );
    

    This includes a page number selector and the component we created earlier. We save the currentPageNumber state in the parent component and provide it to both child components. The ProgrammingQuotes component uses it to fetch pages dynamically.

    However, if you try changing the page number on the app, you will notice that AJAX is not loaded again. And, if you change the initial currentPageNumber state, you will notice that it loads the next page of the API. This is because componentDidMount() only called one time at the first rendering.

    In this case, componentDidUpdate comes into action. We can use it to re-fetch data when the props are changed.

    From React Docs: componentDidUpdate() is invoked immediately after updating occurs. This method is not called for the initial render.

    That’s what we actually need. Let’s use it.

    class ProgrammingQuotes extends React.Component {
    
        constructor(props) {
            // no change
        }
    
        fetchData() {
            // reset
            this.setState({
                isLoaded: false,
                error: null,
                quotes: []
            });
    
            var xhr = new XMLHttpRequest();
    
            xhr.addEventListener("readystatechange", () => {
                    if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                    // request succesful
                    var response = xhr.responseText,
                            json = JSON.parse(response);
    
                this.setState({
                    isLoaded: true,
                    quotes: json
                 });          
            } else {
                    // error
                this.setState({
                    isLoaded: true,
                    error: xhr.responseText
                });
            }
                    }
            });
    
            var page = this.props.currentPageNumber;
    
            xhr.open("GET", "https://programming-quotes-api.herokuapp.com/quotes/page/" + page, true);
            xhr.send();
    
        }
    
        componentDidMount() {
            this.fetchData();
        }
        componentDidUpdate(prevProps) {
            // compare with previous props
            if (prevProps.currentPageNumber !== this.props.currentPageNumber) {
                this.fetchData();
            }
        }
    
        render() {
            // no change
        } 
    
    }
    

    First, we separate the AJAX process as we need need to use it in two life-cycle methods.

    In the componentDidUpdate, we compare the current props with the previous one (this is sent to the function by React). If the currentPageNumber is new, we will re-fetch the data.

    Check these new features on JSFiddle.

    Aborting AJAX Request

    Try changing the page number very fast. You will see that when we change it twice very quickly, it will load both. You can see this more effectively on the browser console.

    We can cancel the fetching request so that this issue won’t happen. Here’s the updated fetchData() method.

    xhr = null;
    
    fetchData() {
        // reset
        this.setState({
            isLoaded: false,
            error: null,
            quotes: []
        });
    
        // abort previous request
        if (this.xhr)
            this.xhr.abort();
    
        var xhr = new XMLHttpRequest();
    
        xhr.addEventListener("readystatechange", () => {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    // request succesful
                    var response = xhr.responseText,
                        json = JSON.parse(response);
    
                    this.setState({
                        isLoaded: true,
                        quotes: json
                     });          
                } else {
                        // error
                    this.setState({
                        isLoaded: true,
                        error: xhr.responseText
                    });
                }
    
                this.xhr = null;
            }
        });
    
        var page = this.props.currentPageNumber;
    
        xhr.open("GET", "https://programming-quotes-api.herokuapp.com/quotes/page/" + page, true);
        xhr.send();
    
        this.xhr = xhr;
    
    }
    

    First, we set a class property xhr to save the current XMLHttpRequest instance. When the next one is called, we can simply abort the previous one. You can also do this with Axios (bit complicated) too.

    Conclusion

    In this article, we discussed how to make AJAX requests (or asynchronous API calls), how to use it in React, where to use it, and some tricks to solve everyday problems. If you like to play with, here’s the final live demo. Also, don’t forget to read the programming quotes.

    Thank you very much for reading! If you have any questions, please let me know.

    Good Bye

    See you in the next article.

    JavaScript react
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Supun Kavinda

      Related Posts

      Streamlining Resource Allocation for Enhanced Project Success

      December 18, 2024

      Conducting Effective Post-Project Evaluations: A Guide

      December 16, 2024

      Strategies for Keeping Projects on Track and Meeting Deadlines

      December 10, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      Interview December 23, 2019

      Ultimate List of JavaScript Interview Questions

      More than 90 questions for Juniors, Middle and Senior Developers. Both practical and theoretical.

      React vs. Angular: Choosing The Right Tools for Your Next Project

      April 4, 2019

      Python enumerate() Explained and Visualized

      September 13, 2019

      Top 10 Bug Tracking Tools for Web Developers and Designers

      December 5, 2019

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Enhancing Customer Feedback Analysis Through AI Innovations

      AI & Automation August 27, 2025

      An Introduction to Pinia: The Alternative State Management Library for Vue.js Applications

      Vue March 29, 2023

      How To Secure Python Web App Using Bandit

      Programming February 13, 2021

      Новичкам

      Wiki May 31, 2016

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Programming

      3. Express.js Lessons. Templating with EJS: Layout, Block, Partials

      B2B Leads

      Leveraging Chatbots for Effective B2B Lead Generation

      Programming

      19. Node.js Lessons. Safe Way to a FS File and Path

      Most Popular

      Effective Strategies for Recruiting Remote Work Talent

      Recruitment

      How to send multiple forms with Ajax (FormData) in Django

      Python

      UX Engineers and the Skills They Need

      Tips
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.