1

I am using React with Typescript and redux with the thunk layer. So, I defined one of my action as follows and it deals with a firestore snapshot listener.

export const getEmployees = () : ThunkAction<void, RootState, null, TableActions> => {

    const db = fire.firestore();

    return async dispatch => {
        db.collection("collection_name").get().then((querySnapshot) => {
            // body
        });
    }
};

and I dispatched it as following when a component will mount.

useEffect(() => {
   dispatch(getEmployees());
},[]);

So, my question is there any best way to unsubscribe the listener when a component will unmount?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Amesh Jayaweera
  • 216
  • 2
  • 11
  • I have found a stackoverflow thread and a link to unsubscribe from firestore which can be useful.[1] https://stackoverflow.com/questions/55905711/componentwillunmount-to-unsubscribe-from-firestore [2] https://brandonlehr.com/reactjs/2018/11/08/unsubscribing-from-firestore-realtime-updates-in-react – Zeenath S N Jul 16 '21 at 09:53

0 Answers0