I have a little bit problem.
I create data tables in my app and Im getting data from api, but from 2 endpoints.
First call returns me (e.g. offers):
{
id: 1,
product_id: 1,
name: 'some name',
etc...
}...
And the second one (eg. products):
{
id: 1,
name: 'some name',
etc...
}...
And I wanna get this first array, but in product_id I want to put product name.
I created this:
const [offers, setOffers] = React.useState(null);
const [products, setProducts] = React.useState(null);
React.useEffect(() => {
fetch(`http://localhost:3000/api/offers`)
.then((res) => res.json())
.then((data) => {
setOffers(data.data);
});
fetch(`http://localhost:3000/api/products`)
.then((res) => res.json())
.then((data) => {
setProducts(data.data);
});
}, []);
But at this point I stopped and I don’t know what to do.
2
Answers
Fetch your products first and then setOffers
Your React code should probably look like this: