Next.js Discord

Discord Forum

What is the best way to store all the products on my store after fetching them from REST API?

Unanswered
Eurasian Collared-Dove posted this in #help-forum
Open in Discord
Eurasian Collared-DoveOP
Hey, I'm trying to set up a shop with printful and next, currently I just have some logic that fetches the products from the printful store via their rest api and an auth token in a GET request route handler, I'm able to get the items and they come in an array simular to:
[
  {
    id: 324635226,
    external_id: '6531e99ba7d205',
    name: 'test beanie',
    variants: 1,
    synced: 1,
    thumbnail_url: 'https://files.cdn.printful.com/files/eac/eac99c0001206a8312ced5025b844e64_preview.png',
    is_ignored: false
  },
//... More Items ...
]

//Which is called via:
const response = await fetch("http://localhost:3000/api/getStoreProducts/");

const json = await response.json();
console.log(json);
//Inside of page.tsx

My question is, what is the best way to store these as objects in an array to be used throughout my application? I would prefer if the syntax could be something similar to json[0].id would result in first product's id so
324635226
in this case.

Is there any way to go about doing this?

2 Replies

Eurasian Collared-DoveOP
thank you in advance for any and all input, I'm still pretty new to next so even if it's links to docs, it is very much appreciated
Cape horse mackerel
You can use https://tanstack.com/query/latest/docs/react/overview or https://swr.vercel.app/. The idea behind those two is the similar, they have a large set of features.
Month ago, my team decided to go with react-query, because it's offering more features and the documentation is really great, developer tools also