React Api Call Function | How to use Api Axios in React

Hello friends today i am talking about React Api function from Axios where i can show you example of code how to fetch api in react

import React, { useState, useEffect } from "react";
import axios from 'axios';
// import image from React;


const Api = () => {
    const [data, setData] = useState([]);

    useEffect(() => {
        axios.get('https://fakestoreapi.com/products').then((res) => {
        setData(res.data);
    })
});
 
  console.log("data->>>>>>>", data);
  return (
    <>
      <section>
        <div className="work-container container">
          <h1 className="main-heading text-center">How does it Work</h1>
          <div className="row">
            {data.map((e) => {
              return (
                <>
                  <div className="col-12 col-lg-3 text-center work-container-subdiv">
                    <h2 className="sub-heading">{e.category}</h2>
                    <p>{e.description}</p>
                    <p>{e.id}</p>
                    <img src={e.image} />
                    <p>{e.price}</p>
                    <p>{e.rating.rate} {e.rating.count}</p>
                    <p>{e.title}</p>
                  </div>
                </>
              );
            })}
          </div>
        </div>
      </section>
    </>
  )
};



export default Api;

Leave a Comment