I have two endpoints. I want to fetch data from them at once and display in one element, for example display data from one table and name from another if the product id is the same. This is how I do this now for one endpoint:
import React, {useState,useEffect} from 'react'
import products from '../apis/apiURLS'
import './Products.css';
const Products = () => {
const [productInfo,setProductInfo] = useState([]);
const fetchProducts = async () =>{
const response = await products.get('/products/')
setProductInfo(response.data)
}
useEffect(()=>{
fetchProducts()
},[])
return (
<div>
<h1 className = "header">Products</h1>
{productInfo.map(products=> {
return (
<div className="productInfo">
{console.log(products)}
<p>Date: {products.date}</p>
</div>
)
})}
</div>
)}
export default Products
EDIT
Products endpoint has those fields: date, product_id and Stock endpoint has fields: product_id, name and product_id is the foreign key to Stock.
I was trying to add another async function for second endpoint:
const [stockInfo, setStockInfo] = useState([]);
const fetchStock = async () =>{
const response = await stock.get('/stock/')
setStockInfo(response.data)
}
useEffect(()=>{
fetchStock()
},[])
But I can't map it correctly to display in one element. What I want to achieve is:
<p>Product: {stock.name}, {products.date}</p>