Skip to header Skip to main navigation Skip to main content Skip to footer
  • Log in
Home
DrupalVIP
Freelancer service with great care and enthusiasm.
  • Home
  • Products
  • Blog
  • Support
    • Dictionary
    • Drupal Resources
    • External Resources

Fetch and display data from API in React js

Breadcrumb

  • Home
  • DrupalVIP Support
  • Fetch and display data from API in React js

All modern browsers come with an inbuilt fetch Web API, which can be used to fetch data from APIs.

import React, { useEffect, useState } from "react"

const UsingFetch = () => {
  const [users, setUsers] = useState([])

  const fetchData = () => {
    fetch("https://jsonplaceholder.typicode.com/users")
      .then(response => {
        return response.json()
      })
      .then(data => {
        setUsers(data)
      })
  }

  useEffect(() => {
    fetchData()
  }, [])

  return (
    <div>
      {users.length > 0 && (
        <ul>
          {users.map(user => (
            <li key={user.id}>{user.name}</li>
          ))}
        </ul>
      )}
    </div>
  )
}

export default UsingFetch

 

There are many ways to fetch data using React

  1. Use the JavaScript Fetch() method
  2. Use the stale-while-revalidate (SWR) method
  3. Use the React Query library
  4. Use the Axios Library
  5. Use the useFetch custom hook from react-fetch-hook

 

with in this post will try the fetch() method, creating example that work and integrate with Drupal 10

 

What is an API?

API stands for Application Programming Interface. 
It enables the exchange of information and functionality between different systems, such as between a website and a server or between different software applications.

You can think of an API as being like a restaurant waiter. 
You don't go inside the kitchen to prepare your meal when you dine at a restaurant. Rather, you inform the waiter of your preferences, and they will take your order to the kitchen team. 
The kitchen team prepares the food and returns it to the waiter, who then delivers it to your table.

An API functions as a waiter for software applications. 
It is a set of rules that lets one program ask another for something it needs. 
It serves as a bridge for software apps to communicate and interact.

 

What is a Hypertext Transfer Protocol (HTTP) request?

When a web browser or a mobile app sends a message to a server, it's known as an HTTP request. 
A HTTP request involves asking the server for specific data or an action and getting a response. 
The server responds by interacting with web pages and services.

Using APIs in software development makes things more flexible and efficient. 
It also enhances security and enables different software systems to work well together.

 

Types of HTTP Requests

We use various HTTP request methods, such as get, post, put, and delete, to get and store data in our database. 
But the most common requests made are the get and post requests.

Let's discuss the meaning of these HTTP request methods:

GET: This method retrieves data from a specific endpoint. 
Think of it as asking for information.

POST: This method sends data to a specific endpoint. 
For example, you can send a message or submit a form. 
The information will be added to the database.

PUT: This method is used to update a record or data value at a designated endpoint. 
You're making changes to existing information.

DELETE: This method erases data from a specific endpoint. 
It's like discarding unnecessary things.

 

Drupal Development

Drupal Development
Code Snippet
import React, { useEffect, useState } from "react";

function handleClick() {
    alert('You clicked node ');
}

export default function RWTaskIssues({nodeid}) {        
    const FetchAPI = '/rwtask/issues?task=' + nodeid + '&op=get';
    console.log("api fetch: " + FetchAPI);
    
    const [issues, setIssues] = useState([]);
    const fetchData = () => {
        fetch(FetchAPI)
            .then(response => {
                return response.json();
            })
            .then(response => {
                console.log("response data: " + response.data.items);
                setIssues(response.data.items);
            });
    };

    useEffect( () => {
        fetchData();
    }, []) ;   
    
    console.log("issues data: " + issues);
    
    return (
        <div className="block block-layout-builder block-field-blocknoderwtaskfield-rwtask-issue">
            <h2 className="block-title">תיאור הבעיה</h2>
            <div className="block-content">               
                {issues.length > 0 && (
                    <div className="field field-name-field-rwtask-issue field-type-text-long field-label-hidden field-items">
                        {issues.map(item => (
                            <div key={item.key} className="field-item" dangerouslySetInnerHTML={{__html: item.value}} />
                        ) ) }
                    </div>
                )}
                <button onClick={handleClick}>
                    Edit
                </button>            
            </div>
        </div>
    );        
}
Read More
Fetch and display data from API in React js
React fetch example - GET/POST/PUT/DELETE with API
React Axios example - Get/Post/Put/Delete with Rest API
How to Fetch API Data in React
Tags
Drupal Support
Drupal 10
React
React Fetch
React useEffect
React useState
React JS
Code Example
Code Snippet
Fetch Data
React Component

The Freelancer Assistance

Fullstack Service

Fullstack Service

Drupal Training

drupal training session

Drupal Development

Drupal Development

Site Management

Site Management

Proactive Maintenance

Proactive Maintenance

Drupal Shared Space

Drupal Shared Space
Review All Services

Buy Hourly Support

 

Drupal platform support for complex websites. The service includes support, training, troubleshooting, fixes, updates, tool creation and module building.
* Minimum order is for 5 hours of support
* For every 20 hours of order you will be entitled to an additional hour of support
* For every 50 hours of order you will be entitled to 5 additional hours of support

 

>> Payment <<

 
cards

מופעל על-ידי paypal

Legal

  • Home
  • Contact
  • Products

Footer menu

  • Home
  • Contact
  • Products

Copyright © 2026 DrupalVIP project of Automatic Frameworks - All rights reserved

Developed and Maintain by Jonathan Ben Hur Freelancer