【发布时间】:2021-06-24 13:10:38
【问题描述】:
我创建了一个 react native 应用程序,并且每次都必须刷新我的屏幕才能从 firebase 获取新添加的数据。我是 firebase 的新手,我认为我可以使用快照来获取当前数据,但我仍然必须在每次创建新事件时刷新我的应用程序才能在此视图上查看所有更新的事件。任何帮助将不胜感激
export default function EventsHostedScreen() {
const navigation = useNavigation();
const [eventsData, setEventsData] = useState([]);
useEffect(() => {
async function fetchData() {
const currentUser = await firebase.auth().currentUser.uid;
result = [];
const eventsCollection = firebase.firestore().collection('events');
eventsCollection.get().then((snapshot) => {
snapshot.docs.forEach((doc) => {
if (doc.exists === true && doc.data().userId !== null) {
if (doc.data().userId === currentUser) {
result.push(doc.data());
}
}
});
setEventsData(result);
});
console.log('RESULT==>', result);
}
fetchData();
}, []);
【问题讨论】:
标签: javascript firebase react-native google-cloud-firestore