Right now I'm setting up a listener every time the component mounts (with a useEffect). However, on the firebase docs it says,
Ideally, your application should set up all the required snapshot listeners soon after opening a connection to Firestore. After setting up your initial snapshot listeners, you should avoid quickly adding or removing snapshot listeners in the same connection.
Is there a way to set up a listener for my profile right when my app loads, and have my profile listen the whole time, even when it unmounts? So when someone goes back to view their profile, they don't set another listener.
export default function Profile() {
const currentUser = useSelector(selectUser);
const [tweets, setTweets] = useState(null);
useEffect(() => {
const unsub = db.collection('tweets').where('uid', '==', `${currentUser.uid}`)
.onSnapshot(snap => {
setTweets(snap.docs.map(doc => ({
id: doc.id,
...doc.data()
})))
})
return () => unsub();
}, [currentUser.uid]);
return (<div className='Profile'> ...rest)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>