Reactjs 在firebase中添加集合文档长度的计数器
我正在开发一个电子商务,前端使用react,后端使用firebase/firestore。 我设法添加/删除产品,并将其存储在属于每个用户的集合中。我想做的是在导航栏和其他组件中有一个计数器,指示登录用户在购物篮中的文档(产品)数量,但由于范围的原因,我无法做到这一点 在products.js中Reactjs 在firebase中添加集合文档长度的计数器,reactjs,firebase,google-cloud-firestore,Reactjs,Firebase,Google Cloud Firestore,我正在开发一个电子商务,前端使用react,后端使用firebase/firestore。 我设法添加/删除产品,并将其存储在属于每个用户的集合中。我想做的是在导航栏和其他组件中有一个计数器,指示登录用户在购物篮中的文档(产品)数量,但由于范围的原因,我无法做到这一点 在products.js中 const addToBasket = () => { db.collection('users').doc(user?.uid).collection('basket')
const addToBasket = () => {
db.collection('users').doc(user?.uid).collection('basket').add({productId: id, title: title,
cont: content, img: image, price: price})
}
在checkoutProduct.js中
const removeFromBasket = () => {
firestore().collection('users').doc(user?.uid).collection('basket').doc(id.toString()).delete();
}
在checkout.js中
useEffect(() => {
if(user){
firestore().collection('users').doc(user?.uid).collection('basket').get().then(snapshot => {
setProducts(snapshot.docs.map((doc) => {return {...doc.data(), id: doc.id} }))
})
}
}, [])
您可以从中获取从
get()
返回的文档数:
firestore().collection('users').doc(user?.uid).collection('basket').get().then(snapshot=>{
setProducts(snapshot.docs.map((doc)=>{return{…doc.data(),id:doc.id}}))
setCartItemCount(snapshot.size);//您可以从中获取从get()
返回的文档数:
firestore().collection('users').doc(user?.uid).collection('basket').get().then(snapshot=>{
setProducts(snapshot.docs.map((doc)=>{return{…doc.data(),id:doc.id}}))
setCartItemCount(snapshot.size);//这是需要在前端处理的常见问题。
一种解决方案是将信息存储在上下文中
从概念上讲,您可以做的一件事是在成功写入数据库时更新该上下文,以Firestore为例
因此,这样您就可以在后端更新时更新前端。
这可以通过快照上的观察者来完成
阅读以下内容:这是一个需要在前端处理的常见问题。
一种解决方案是将信息存储在上下文中
从概念上讲,您可以做的一件事是在成功写入数据库时更新该上下文,以Firestore为例
因此,这样您就可以在后端更新时更新前端。
这可以通过快照上的观察者来完成
阅读此文:如前所述,有许多方法可以解决此问题。因此,让我们详细介绍一下他的useContext提案。根据我在应用程序中处理Firebase的方式,我对Firebase有一个单独的类,因为它应该是一个单独的类:
class Firebase {
constructor() {
app.initializeApp(config);
this.db = app.database();
this.auth = app.auth();
}
// *** Auth API ***
createUserWithEmailAndPassword = (email, password) =>
this.auth.createUserWithEmailAndPassword(email, password);
signInWithEmailAndPassword = (email, password) =>
this.auth.signInWithEmailAndPassword(email, password);
signOut = () => this.auth.signOut();
sendPasswordResetEmail = email => this.auth.sendPasswordResetEmail(email);
updatePassword = password => this.auth.currentUser.updatePassword(password);
onAuthUserListener = (next, fallback) =>
this.auth.onAuthStateChanged(user => {
if (user) {
this.user(user.uid)
.once("value")
.then(snapshot => {
const dbUser = snapshot.val();
if (!dbUser.roles) {
dbUser.roles = {};
}
next({
uid: user.uid,
email: user.email,
...dbUser
});
});
} else {
fallback();
}
});
// *** User API ***
user = uid => this.db.ref(`/users/${uid}`);
users = () => this.db.ref(`users`);
}
export default Firebase;
然后我会为它创建一个上下文:
const FirebaseContext = React.createContext(null);
有许多方法可以使用该上下文,或者与useContext一起使用,或者与FirebaseContext.Provider/Consumer一起使用
我通常在FirebaseProvider中包装我的应用程序:
ReactDOM.render(
<FirebaseContext.Provider value={new Firebase()}>
<App />
</FirebaseContext.Provider>,
document.getElementById('root'));
ReactDOM.render(
在上下文中查看更多内容。如前所述,有许多方法可以解决此问题。因此,让我们详细介绍一下他的useContext提案。我在应用程序中处理Firebase的方式,我为Firebase提供了一个单独的类,因为它应该是一个单独的类:
class Firebase {
constructor() {
app.initializeApp(config);
this.db = app.database();
this.auth = app.auth();
}
// *** Auth API ***
createUserWithEmailAndPassword = (email, password) =>
this.auth.createUserWithEmailAndPassword(email, password);
signInWithEmailAndPassword = (email, password) =>
this.auth.signInWithEmailAndPassword(email, password);
signOut = () => this.auth.signOut();
sendPasswordResetEmail = email => this.auth.sendPasswordResetEmail(email);
updatePassword = password => this.auth.currentUser.updatePassword(password);
onAuthUserListener = (next, fallback) =>
this.auth.onAuthStateChanged(user => {
if (user) {
this.user(user.uid)
.once("value")
.then(snapshot => {
const dbUser = snapshot.val();
if (!dbUser.roles) {
dbUser.roles = {};
}
next({
uid: user.uid,
email: user.email,
...dbUser
});
});
} else {
fallback();
}
});
// *** User API ***
user = uid => this.db.ref(`/users/${uid}`);
users = () => this.db.ref(`users`);
}
export default Firebase;
然后我会为它创建一个上下文:
const FirebaseContext = React.createContext(null);
有许多方法可以使用该上下文,或者与useContext一起使用,或者与FirebaseContext.Provider/Consumer一起使用
我通常在FirebaseProvider中包装我的应用程序:
ReactDOM.render(
<FirebaseContext.Provider value={new Firebase()}>
<App />
</FirebaseContext.Provider>,
document.getElementById('root'));
ReactDOM.render(
要在上下文中查看更多内容。您想要一个可视化篮子中项目数量的组件,然后在多个位置重用该组件吗?还是只想让数据在多个不同的位置可视化?是的,第二个选项,我想让数据在不同的位置可视化您想要一个可视化数量的组件吗然后在多个地方重复使用该组件?还是只想让数据在多个不同的地方可视化?是的,第二个选项,我想让数据在不同的地方可视化