🌐
GitHub
github.com › Zaka112 › Frontend-project-FakeStore-API-
GitHub - Zaka112/Frontend-project-FakeStore-API-: Frontend ecommerce project (Fakestore API)
Fetch data from this API: https://api.escuelajs.co/api/v1/products · The user can search product by name · Get the product detail by this API: https://api.escuelajs.co/api/v1/products/name · When the user click to product image or a button ...
Author   Zaka112
🌐
Escuelajs
api.escuelajs.co › docs
Swagger UI - Platzi Fake Store API 1.0 OAS 3.0
We cannot provide a description for this page right now
🌐
Platzi
fakeapi.platzi.com › en › rest › products
Products | Platzi Fake Store API
You can access the list of 50 products by using the /products endpoint. Terminal window · [GET] https://api.escuelajs.co/api/v1/products ·
🌐
Reddit
reddit.com › r/reactjs › building a decoupled event management system in react: a clean and scalable approach.
r/reactjs on Reddit: Building a Decoupled Event Management System in React: A Clean and Scalable Approach.
December 30, 2024 -

Hi everyone!

I’ve recently written a blog post about building a decoupled event management service in React.

I've tried to explain the implementation of an Observer pattern using a custom Observer class, how to manage events efficiently with the Event Mediator Provider, and how to make your React components more modular and scalable.

I’d really appreciate hearing your thoughts and feedback, especially if you’ve implemented similar patterns or have suggestions for improvement!

Here is link.

🌐
Platzi
fakeapi.platzi.com
Platzi Fake Store API | Platzi Fake Store API
[GET] https://api.escuelajs.co/api/v1/products · $44 · $10 · $69 · $90 · $79 · $25 · $79 · $98 · $61 · $86 · [GET] https://api.escuelajs.co/api/v1/categories · Clothes · Electronics · Furniture · Shoes · Miscellaneous · [GET] https://api.escuelajs.co/api/v1/users ·
🌐
AzamSharp
azamsharp.com › 2022 › 10 › 30 › evolving-client-server-swiftui.html
Evolving Client Server Swiftui | AzamSharp
October 30, 2022 - import React, { useState, useEffect } from 'react' function App() { const [products, setProducts] = useState([]) useEffect(() => { fetchProducts() }, []) const fetchProducts = async () => { const response = await fetch('https://api.escuelajs.co/api/v1/products?offset=0&limit=10') const products = response.json() setProducts(products) } const productItems = products.map(product => { return `<li>{product.title}</li>` }) return ( <div> {productItems} </div> ); }
🌐
Stack Overflow
stackoverflow.com › questions › 78410175 › the-title-property-appears-to-me-as-a-type-that-does-not-exist-and-i-dont-know
reactjs - The title property appears to me as a type that does not exist and I don't know what to do? - Stack Overflow
... You are missing quotes around the string in your fetch. It should be: const data = await fetch("https://api.escuelajs.co/api/v1/categories/${categoryToSearch}/products") .then(response => response.json());
🌐
Medium
medium.com › @adityakuagrawal › mastering-api-integration-in-flutter-tips-and-tricks-for-success-042afe5455a9
Mastering API Integration in Flutter: Tips and Tricks for Success 🚀 | by Aditya Agrawal | Medium
April 24, 2024 - class ProductApi { List<ProductModel> products = []; String baseUrl = "https://api.escuelajs.co/api/v1/"; // The fetchProductList method asynchronously retrieves the list of products.
🌐
Medium
medium.com › @amolakapadi › how-to-fetch-api-in-react-native-and-render-using-flatlist-cb833d14947a
How To Fetch API In React Native And Render Using Flatlist | by Amol kapadi | Medium
May 8, 2024 - In this code fetches a list of products from an API and displays them in a two-column layout using React Native’s FlatList component. Each product is rendered as a card containing its title, image, and price. API END POIND : https://api.escuelajs.co/api/v1/products
Find elsewhere
🌐
GitHub
github.com › platzi › laboratorio-fakestore
GitHub - platzi/laboratorio-fakestore
const $app = document.getElementById("app"); const $observe = document.getElementById("observe"); const API = "https://api.escuelajs.co/api/v1/products";
Starred by 13 users
Forked by 60 users
Languages   JavaScript 69.0% | CSS 20.6% | HTML 10.4% | JavaScript 69.0% | CSS 20.6% | HTML 10.4%
🌐
GitHub
github.com › platzi › js-challenge
GitHub - platzi/js-challenge
const $app = document.getEleme....escuelajs.co/api/v1/products"; Tenemos una función llamada getData encargada de hacer solicitudes Fetch a una API y de construir un nuevo elemento en el DOM: const getData = (api) => { ...
Starred by 47 users
Forked by 432 users
Languages   JavaScript 69.0% | CSS 20.6% | HTML 10.4% | JavaScript 69.0% | CSS 20.6% | HTML 10.4%
🌐
Medium
medium.com › @vishnusatheeshpulickal › infinite-scroll-techniques-in-react-199d42c3af04
Infinite Scroll Techniques in react | by Vishnu Satheesh | Medium
May 23, 2024 - import React, { useState, useEffect } from "react"; import InfiniteScroll from "react-infinite-scroll-component"; import axios from "axios"; import ProductCard from "./ProductCard"; import Loader from "./Loader"; const InfiniteScrollExample1 = () => { const [items, setItems] = useState([]); const [hasMore, setHasMore] = useState(true); const [index, setIndex] = useState(2); useEffect(() => { axios .get("https://api.escuelajs.co/api/v1/products?offset=10&limit=12") .then((res) => setItems(res.data)) .catch((err) => console.log(err)); }, []); const fetchMoreData = () => { axios .get(`https://api.escuelajs.co/api/v1/products?offset=${index}0&limit=12`) .then((res) => { setItems((prevItems) => [...prevItems, ...res.data]); res.data.length > 0 ?
Top answer
1 of 2
3

Your default state for products is [], so the conditional render data.products in ProductDetail.js always return true so you can change default state for products is null

const [products, setProducts] = useState(null);
2 of 2
2

The first answer is correct, so I will not duplicate it, but I see room for improvement in your code/example.

Your useGetProducts hook is very easy to break and hard to reuse. If you will pass the wrong URL or the structure of the API will change it will break your code. Also, the hook is not very generic, cause you will need to create similar fn for each entity. My suggestion. Use react-query and separate functions for calling API. So it will look like this.

import { useQuery } from 'react-query'
import axios from 'axios'

export default function ProductPage() {
  const productResponse = useQuery('exchanges', () => getProduct('6'))
  const { isLoading, isError, data: product } = productResponse

  return (
    <div>
      {isLoading && <div>Loading...</div>}
      {isError && <div>Something went wrong :(</div>}

      {product && (
        <div>
          <h1>Product title: {product.title}</h1>

          <p>
            {product.images.map(imageSrc => (
              <img key={imageSrc} src={imageSrc} alt="" />
            ))}
          </p>
        </div>
      )}
    </div>
  )
}

interface Product {
  id: string
  title: string
  images: string[]
}

function getProduct(id: string): Promise<Product> {
  return axios
    .get(`https://api.escuelajs.co/api/v1/products/${id}`)
    .then(r => r.data)
}

PS. react-query requires additional configuration ( context provider, config, etc ). Please look into docs on how to use it.

🌐
GitHub
github.com › platzi › escuela-js
GitHub - platzi/escuela-js
Contribute to platzi/escuela-js development by creating an account on GitHub.
Starred by 40 users
Forked by 35 users
Top answer
1 of 2
2
  1. Don't use ref when you use the option api, you're mixin up option api and composition api (the setup function)

  2. Move your api call to a method and call it from both created and handleClick

export default {
  components: {
    Clothes,
    Shoes,
  },
  data() {
    return {
      product: 1,
      stores: [],
      errors: [],
    };
  },
  methods: {
    fetchProduct(productId) {
      return axios.get(`https://api.escuelajs.co/api/v1/categories/${productId}/products`)
        .then((res) => {
          this.stores = res.data;
        })
        .catch((e) => {
          this.errors.push(e);
          console.error(e);
        });
    },
    handleClick(productId) {
      this.product = productId;
      this.fetchProduct(productId);
    },
  },

  //Here is the Api call.
  async created() {
    await this.fetchProduct(this.product);
  },
};
2 of 2
0

You need to wait for sync call :

const app = Vue.createApp({
  data() {
    return {
      product: 1,
      stores: [],
      errors: [],
    };
  },
  methods: {
    async fetchProduct(productId) {
      await axios.get(`https://api.escuelajs.co/api/v1/categories/${productId}/products`)
        .then((res) => {
          this.stores = res.data;
        })
        .catch((e) => {
          this.errors.push(e);
          console.error(e);
        });
    },
    handleClick(productId) {
      this.product = productId;
      this.fetchProduct(productId);
    },
  },
  async created() {
    await this.fetchProduct(this.product);
  },
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.2.2/axios.min.js" integrity="sha512-QTnb9BQkG4fBYIt9JGvYmxPpd6TBeKp6lsUrtiVQsrJ9sb33Bn9s0wMQO9qVBFbPX3xHRAsBHvXlcsrnJjExjg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<div id="demo">
  <button @click="handleClick(3)">get productid 3</button>
  {{stores}}
</div>

🌐
Built In
builtin.com › articles › react-infinite-scroll
React Infinite Scroll: A Guide | Built In
Its purpose is to handle additional data retrieval from the API when it is invoked. const fetchData = useCallback(async () => { if (isLoading) return; setIsLoading(true); axios .get(`https://api.escuelajs.co/api/v1/products?offset=${index}0...
🌐
Fake Store API
fake-store-api-docs.vercel.app › en › rest › auth-jwt
Autorización con JWT | Fake Store API
[POST] https://api.escuelajs.co/api/v1/auth/login # Body { "email": "angel@gmail.com", "password": "angel1234" } Si el email y la contraseña no coinciden, el servidor responderá con un error 401. Si el email y la contraseña coinciden, el servidor respondera con un 200 OK y incluirá en el ...