I currently have a useFirestore hook for adding, deleting and updating documents in Firestore in a React social media app
I can then call those functions directly from a component using the hook which is great:
const { addDocument, response } = useFirestore('events');
const handleSubmit = (e) => {
addDocument({ uid: user.uid, title, location });
};
but I have some situations where i'm wondering is it better to not use a hook as it clutters the component code. An example is for friend requests:
- If Mary requests John to be a friend I need to add 2 documents to firestore, 1 for Mary and 1 for John.
- If John then accepts the request I need to delete both those documents and then add 2 new documents under something like
confirmedFriendsfor both John and Mary
I don’t much like having to add 4 different firestore functions to the component handler:
const handleAcceptRequest = (…) => {
e.preventDefault();
deleteDocument(…);
deleteDocument(…);
addDocument(
….
….
….
);
addDocument(
….
….
….
);
};
I suppose I am getting confused as to whether you should always use a hook when interacting with firestore in a React project?
I think in this situation I would rather create something like a service.js file to handle multiple interactions in firestore for one 'action' like accepting a friend request. But I can only use the hooks in a component so is it a good solution to add a new service.js file and repeat logic to add and delete to firestore for the above situation?
Then it leads me to think that why not just change the useFirestore hook file completely to a standard file of functions that are reusable from outside components and then I can keep them seperate from components?
Is there a massive downside? So should I use functions or hooks or a hybrid of both? Any advice is much appreciated!