1

I'm wondering whether it's possible to have changes in a Firestore DB automatically reflected in a React app. That is -- if I have the correct terminology -- whether I can bind the two together, with the Firestore DB acting as a model and the React app acting as the view.

Failing using something like WebSockets, I can't think of a way that doesn't involve some sort of polling.

I did come across react-redux-firebase, which allows to connect Firestore to Redux. But on a first glance, I can't tell whether this means that changes in Firestore are automatically transmitted to Redux, that is whether react-redux-firebase does some sort of polling, etc, on its own. Or whether you have to query the Firestore data yourself.

(Full disclosure: reflecting changes in React from a Firestore DB is a small part of a coding challenge I'm working on, but I figure it's fair game to ask on Stack Overflow to see if I'm heading down a viable path, since this is what I'd do in 'real life' in any case. I suspect I'll need to do the polling myself, but wanted to see if there were any library, like react-redux-firebase, that would 'push' the data from Firestore to React/Redux automatically).

Cerulean
  • 5,543
  • 9
  • 59
  • 111
  • 3
    Sure is: use a listener as shown here, https://firebase.google.com/docs/firestore/query-data/listen, and then in `onSnapshot` set the data to the state of your react component, which then triggers a rerender of the UI with the updated content. If you have problems making this work, edit your question to show what you've done. – Frank van Puffelen Jan 10 '21 at 01:38
  • @FrankvanPuffelen -- One question, I have a config object which contains private key info, etc. In a Node app, I can access the firestore account using firebase-admin, ` firebaseAdmin.initializeApp({ credential: firebaseAdmin.credential.cert(configObject)});`, but when I try to use the Firebase Javascript SDK (can't use firebase-admin in React) I don't see any similar method. That is, using the SDK, or react-redux-firebase, they all seem to want a different object, using the API key, leaving auth for later. How to login automatically, using the credentials that have the private key? – Cerulean Jan 10 '21 at 15:57
  • You can't securely use the Admin SDK in client-side code. It is starting to sound like a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) at this point. – Frank van Puffelen Jan 10 '21 at 17:34
  • Fair enough, I think this is a rabbit hole. Thanks for your help though. I think the problem is that was a full-stack test, not a Firebase test, and I'm getting lost in implementation details of Firebase, when I don't have sufficient privileges to follow any of the routes suggested by the documentation. I've implemented a polling solution where the React front polls an express backend that CAN log into the Firebase DB through the admin -- that's the best I can do for now... – Cerulean Jan 10 '21 at 18:10
  • Did the initial comment posted by Frank van Puffelen answered your question? – Daniel Ocando Jan 12 '21 at 14:39
  • I believe so, but could not test it since I did not have the necessary access to the Firebase account. – Cerulean Jan 13 '21 at 00:18

0 Answers0