🌐
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
Discussions

Building a Decoupled Event Management System in React: A Clean and Scalable Approach.
Using an interface and prefixing it with I is an anti-pattern. An interface is the thing and we shouldn’t care that it is an interface but using the I prefix promotes the kind of thinking that the interface is the most important aspect of the definition when it is not. We can see the result of this anti pattern in the fact that there is only one concrete implementation called Observer. So IObserver adds nothing to the design. When using an interface design pattern the interface should be named the thing thing and the concrete implementations should define what specific variation they define. So for example Observer would be the interface and the concrete implementation might be EventBasedObserver. If you cannot come up with a name for the concrete implementation it probably means you don’t really have a use for the interface pattern which is the case with this code. Further evidence that the interface pattern is unnecessary is that the calling code imports and uses only the concrete class not the interface. More on reddit.com
🌐 r/reactjs
30
14
December 30, 2024
reactjs - The title property appears to me as a type that does not exist and I don't know what to do? - Stack Overflow
I am doing a dynamic route with astro and react and when doing a fetch to obtain the array of objects it tells me that the Prduct.title property is never and I don't know why src/pages/category/[ca... More on stackoverflow.com
🌐 stackoverflow.com
How do i make another Api call onclick in vue js
I am new to vue js and i have this component, i am trying to make another API call on click of one of the list options. More on stackoverflow.com
🌐 stackoverflow.com
reactjs - Problem when rendering image in React component - Stack Overflow
I´m getting an error when trying to render an image in component. I paste the code here. Is it possible that I need a babel or webpack plugin? In this component, the image rendering works fine: imp... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 ·
🌐
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());
🌐
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%
🌐
AzamSharp
azamsharp.com › 2022 › 10 › 30 › evolving-client-server-swiftui.html
Evolving SwiftUI Architecture for Client/Server Apps
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> ); }
Find elsewhere
🌐
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
🌐
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.
🌐
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 ?
🌐
GitHub
github.com › MartinIglesias86 › api-consume
GitHub - MartinIglesias86/api-consume: Platzi challenge
const $app = document.getEleme... "https://api.escuelajs.co/api/v1/products"; Función llamada 'getData' que se encarga de hacer Fetch a una API y debe de construir un elemento nuevo en el DOM....
Forked by 482 users
Languages   JavaScript 75.8% | CSS 15.2% | HTML 9.0% | JavaScript 75.8% | CSS 15.2% | HTML 9.0%
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>

🌐
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
🌐
GitHub
github.com › wayala-designli › Fake-Store
GitHub - wayala-designli/Fake-Store
You need to create the .env, .env.development and .env.production files in the root of your project, following the example of .env.example to add the variables, in this case only BASE_URL is needed, but because it's a test and a public API, the value is https://api.escuelajs.co/api/v1 ·
Author   wayala-designli
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.