简体   繁体   English

Nextjs App如何在使用Firestore查询后处理本地数据存储?

[英]Nextjs App how to handle local data storage after querying with Firestore?

I'm developing a react e-commerce app with Firestore database.我正在使用 Firestore 数据库开发一个反应电子商务应用程序。 When the app loads it fetches first 50 products (each one is a Firestore doc), and renders the product card with the data.当应用程序加载时,它会获取前 50 个产品(每个产品都是 Firestore 文档),并使用数据呈现产品卡片。 My question is where to store this data for easy access in case of page refresh, navigation to say to order page and come back to the products page, close the tab and open again etc.我的问题是在哪里存储这些数据以便在页面刷新、导航到订单页面并返回产品页面、关闭选项卡并再次打开等情况下轻松访问。

I'm asking this because when I monitor my Firestore usage for following;我问这个是因为当我监控我的 Firestore 使用情况时;

  1. I open products page and load 50 products, go to second page load another 50 etc.我打开产品页面并加载 50 个产品,go 到第二页加载另外 50 个等。
  2. Do filtering get another 50 products, remove filters, get the first 50 products again.过滤得到另外 50 个产品,删除过滤器,再次得到前 50 个产品。
  3. Go to orders page come back get 50 products. Go 到订单页面回来获得 50 件产品。

All in all one session can easily reach thousands of doc reads, lots of data use and longer load times.总而言之,session 可以轻松实现数千次文档读取、大量数据使用和更长的加载时间。 So I think I`m missing a point here.所以我想我在这里漏掉了一点。 There should be a way to store all these data without making the app slower and data usage more expensive.应该有一种方法来存储所有这些数据,而不会使应用程序变慢并且数据使用成本更高。 What would be a good approach here to save the data in session and full refresh, close the tab and come back etc. ?将数据保存在 session 中并完全刷新、关闭选项卡并返回等的好方法是什么?

PS I'm using Next.js and Firestore, data fetching is done on the client side via Firestore web library. PS 我正在使用 Next.js 和 Firestore,数据获取是通过 Firestore web 库在客户端完成的。

As soon as you get the data from firebase simply store it over local storage as follows,从 firebase 获取数据后,只需将其存储在本地存储中,如下所示,

considering you get the data of component load like in a useEffect and store it in a state name as product.考虑到您像在 useEffect 中一样获取组件负载数据并将其存储在 state 名称中作为产品。 Now my approach would be to look for any update in product variable through useEffect and simply update the local storage现在我的方法是通过 useEffect 查找产品变量中的任何更新,并简单地更新本地存储

const [product,setproduct] = useState([]); // containing array of product
useEffect(() => {
   if(product.length>0) localStorage.setItems('products',JSON.stringify(product));
  }, [product]);

That way you have it on localstorage and use it anywhere around the application.这样你就可以在本地存储中使用它,并在应用程序的任何地方使用它。 Also in other components while calling firestore to get product make sure to check local storage first.同样在其他组件中,在调用 firestore 获取产品时,请务必先检查本地存储。 If there is already some data stored over local storage, dont go up for calling firestore to get data.如果已经有一些数据存储在本地存储上,不要 go 调用 firestore 来获取数据。

That will reduce you API calls and along side increase the performance of the application.这将减少您的 API 调用并同时提高应用程序的性能。

Voila.瞧。 I hope it solves your purpose: :)我希望它能解决你的目的::)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM